Named elements of a table

Find quick help here to get you started with Hollywood
Post Reply
peceha
Posts: 111
Joined: Tue Dec 13, 2016 8:39 am
Location: Poland

Named elements of a table

Post by peceha » Mon Jul 31, 2017 10:42 am

That is something I just encountered and it made me wonder.
In the manual you can read:
You can access named elements of a table also in two ways:
b = a["name"]
b = a.name
Up to now, at the beginning of my script I had following table declaration:

Code: Select all

mapSettings	= {
	["minRmW"]		= 3,
	["minRmH"]		= 3,
	["maxRms"]		= 5,
	["rmChnc"]		= 80,
	["sparse"]			= 50,	
	["loops"]			= 50,	
	["rndDirTreshold"]	= 80
}
and I decided to replace it with this:

Code: Select all

mapSettings	= {
	minRmW			= 3,
	minRmH			= 3,
	maxRms			= 5,
	rmChnc			= 80,
	sparse			= 50,	
	loops			= 50,	
	rndDirTreshold	= 80
}


And now I had a lot of errors: "table field was not initialized" and it turned out that my old syntax was causing it:

Code: Select all

aLoc=bspSplit(0,0,#W,#H,mapSettings["maxRms"])
or from another table:

Code: Select all

colorReq.shadowOnOff=color["shadowOnOff"]
so I changed these entries to:

Code: Select all

mapSettings.maxRms
color.shadowOnOff
and it is working.

Have I misunderstood something here?

SamuraiCrow
Posts: 241
Joined: Fri May 15, 2015 5:15 pm
Location: Ft. Collins, Colorado USA

Re: Named elements of a table

Post by SamuraiCrow » Mon Jul 31, 2017 12:56 pm

The quotes around the variable name mean that the name of the element is literally what's between the quotes, not the contents of the variable with that name.

peceha
Posts: 111
Joined: Tue Dec 13, 2016 8:39 am
Location: Poland

Re: Named elements of a table

Post by peceha » Mon Jul 31, 2017 5:48 pm

Yes , I'm aware of this.
What I don,t understand is why my program gave me plenty errors about table fields not being initialized - all I did was a change from (example):

t["field"]=0
to
t.field=0

then everytime there was something like function(t["field"]) in my script , I had an error message, so had to change it to function(t.field)

t.field is the same as t["field"], right? - i just checked it and it seems to be right.

User avatar
Allanon
Posts: 461
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

Re: Named elements of a table

Post by Allanon » Mon Jul 31, 2017 5:58 pm

Hi peceah,

looking to one of your example :

Code: Select all

mapSettings   = {
   ["minRmW"]      = 3,
   ["minRmH"]      = 3,
   ["maxRms"]      = 5,
   ["rmChnc"]      = 80,
   ["sparse"]         = 50,   
   ["loops"]         = 50,   
   ["rndDirTreshold"]   = 80
}
This is not the same of:

Code: Select all

mapSettings   = {
   minRmW         = 3,
   minRmH         = 3,
   maxRms         = 5,
   rmChnc         = 80,
   sparse         = 50,   
   loops         = 50,   
   rndDirTreshold   = 80
}
because there is no case distinction in table indexes, so for example:

Code: Select all

; SMALLER EXAMPLE
 test = {}
 test.Field1 = "Hello"  ; <- The real index stored is all in lowercase!
 DebugPrint(test.Field1)     ; This will work
 DebugPrint(test["field1"])  ; This will work
 DebugPrint(test["Field1"])  ; This will trow an error
If you want to maintain case distinction you have to store and retrieve items using ["..."] :

Code: Select all

 test = {}
 test["Field1"] = "Hello"
 DebugPrint(test["Field1"])  ; This will work
 DebugPrint(test.Field1)     ; This will trow an error
 DebugPrint(test["field1"])  ; This will trow an error
I hope this helps :)
----------------------------
[Allanon] Fabio Falcucci
AMC - Creative Development // Docs Site // Support Forum
Support me on Patreon for Hollywood libraries!

peceha
Posts: 111
Joined: Tue Dec 13, 2016 8:39 am
Location: Poland

Re: Named elements of a table

Post by peceha » Mon Jul 31, 2017 6:02 pm

WOW :)

Thank You - that helped for sure !!!

User avatar
Allanon
Posts: 461
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

Re: Named elements of a table

Post by Allanon » Mon Jul 31, 2017 6:42 pm

Great! :D
----------------------------
[Allanon] Fabio Falcucci
AMC - Creative Development // Docs Site // Support Forum
Support me on Patreon for Hollywood libraries!

Post Reply