[12 Apr 2007] helpful hints on tables

Contains all messages from the Hollywood mailing list between 01/2006 and 08/2012
Locked
Dwayne

[12 Apr 2007] helpful hints on tables

Post by Dwayne »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Thu, 12 Apr 2007 01:35:14 +1000

Hi again,

Just thought I would let you know that I have found out what my issue was here.. I should have started reading the file before I entered the first iteration of the function.

I have also been experimenting more with the tables and found a solution for what I wanted and I also started to combine functions with the tables.. Very easy once you get the hang of it and should be qote powerful to boot.

For other people that are interested so I worked out the following for building the tables (please correct me if I am wrong or if there is a better way but:

More challenge was to build the hierarchy of the tables dynamically by reading in a file and I have workded out a few basic principles to do this...

So the end goal for me was to have a table like

resources.colors.white

Seems fairly straight forward but at each node you need to initialise the next node or dimension of the table by using the table declaration.. Once it is declared with as an empty table you

Code: Select all

resources={}
resources.colors={}
resources.colors.white={}
Now because I was using a string variable that changed as I was reading through the file.. I struggled to work out how to apply this based on the examples.. in the end it was a matter of removing the .

So you do this:

Code: Select all

v1$="colors"
v2$="white"
resources={}
resources[(v1$)]={}
resources[(v1$)][(v2$)]={}
And you can even mix the two i.e. the last line could be replaced by

Code: Select all

resources.colors[(v2$)]={}

or 

resources[(v1$)].white={}
Note the placement of the period.. This was not self evident at least to me and required quite a bit of trial and error.

Because I am reading a file that has open and close tags in various locations that actually define the hierarchy that I am using for my table I needed to find someway of building this.. So you can follow what I mean the config files are something like the following:

Code: Select all

<resources>
<colors>
<white a="255" r="255" g="255" b="255" />
<fonts>
<main-font>
     <typeface="Arial size="16" style="bold" />
</main-font>
</fonts>
</colors>
</resources>
The end table will end up being something like the following

Code: Select all

resources.colors.white.argb=$FFFFFFFF
resources.colors.white.rgb=$FFFFFF
resources.fonts.main_font.typeface="Arial.font"
resources.fonts.main_font.size=16
resources.fonts.main.style="#BOLD"
To build the table correctly I was storing the tags colors, white, argb, rgb, fonts, main_font, typeface etc in an array so I can access the values by their respective array position in the final array as I'm building the table..

i.e.

Code: Select all

tags={"colors", "white", "rgb"}
so this means

Code: Select all

tags[0]="colors" tags[1]="white" tags[2]="rgb"


and for the next line

Code: Select all

tags={"colors", "white", "argb"} so tags[0]
tags[0]="colors" tags[1]="white" tags[2]="argb" 
etc...

No because I don't know how many tags are going to be in each line I couldn't arbitrarily build my table by 4 nodes and adding them.. I found out something very important.. if you print out a table entry that is a node or dimension to another table and not an entry that has a value i.e. print resources.colors <- a node rather than resources.colors.white.rgb <-which has been assigned a value then you a returned table: and a memory reference..

Well this in itself this is not revelatory but consider the following.

We have an initialsed table called resources which is a multidimensional table with other nodes or tables

resources.colors.white etc.

well if you do the following

newtable=resources then newtable will actually point to the start of the resources table.. and you can use

newtable.colors.white etc.. as it references the same location as does the resources table... but the kicker is if you do this...

newtable=resources.colors then newtable will reference the same location as recources.colors so you could theoretically do this

newtable.white etc..

Now this means that I have access to any node in the table.. So getting bac to my original dilemma of how to contrast the table dynamically... Two loops oughta do the trick..

Code: Select all

tags={"colors", "white", "rgb"}

For loop1=0 to totaltags     /* we can assume totaltags has been calculated but for the sake of the example above it would equal 2 */

   tmptable=resources

   For loop2=0 to (loop1-1)
            tmptable=tmptable[(tags[loop2])] 
   Next

   tmptable[(tags[loop1])]={} /* initialise the new node or table with the name from the tags table */

Next
I will add an additional error check to see whether or not the table already exists before creating it.. in case we encounter a tag with the same name later on in the file..

I'm sorry for the long post but I hope somebody gets some use out of it and it helps them learn a little bit more about tables without having to go through as much trial and error over small things as I did.. I apologise for any inaccuracies listed above and would like to know of any corrections if my ramblings are a little bit misleading. If you have got something out of this let me know and I will continue to post tidbits when I find learn some more so that others may benefit from my blundering..

If this has already been discussed in more details or if there are references readily available that expands on what I have written I would also like to know as I'm sure I have only started at the tip of the iceberg with tables or even my understanding of the lua script language.

Regards,

Dwayne
User avatar
airsoftsoftwair
Posts: 5832
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

[12 Apr 2007] Re: helpful hints on tables

Post by airsoftsoftwair »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Thu, 12 Apr 2007 12:25:27 +0200
Hi again,

Just thought I would let you know that I have found out what my issue was here.. I should have started reading the file before I entered the first iteration of the function.

I have also been experimenting more with the tables and found a solution for what I wanted and I also started to combine functions with the tables.. Very easy once you get the hang of it and should be qote powerful to boot.

For other people that are interested so I worked out the following for building the tables (please correct me if I am wrong or if there is a better way but:

More challenge was to build the hierarchy of the tables dynamically by reading in a file and I have workded out a few basic principles to do this...

So the end goal for me was to have a table like

resources.colors.white

Seems fairly straight forward but at each node you need to initialise the next node or dimension of the table by using the table declaration.. Once it is declared with as an empty table you

resources={}

resources.colors={}

resources.colors.white={}
You could also write

Code: Select all

resources = {colors = {}, white = {}}
Or with strings:

Code: Select all

resources = {["colors"] = {}, ["white"] = {}}
There are lots of syntax possibilities with tables.
Now because I was using a string variable that changed as I was reading through the file.. I struggled to work out how to apply this based on the examples.. in the end it was a matter of removing the .
Yep. By the way, table access via strings is the only thing which is currently case sensitive in Hollywood, e.g.

Code: Select all

table["COLORS"]
table["colors"]
address two different table items. I will change this in the future to case insensitive addressing, so better don't depend on it.
Note the placement of the period.. This was not self evident at least to me and required quite a bit of trial and error.
There's an introduction to tables in the Hollywood documentation. However, it stops at a certain point of complexity. But once you get the idea of tables, it should be fairly easy to do whatever you want with them.
No because I don't know how many tags are going to be in each line I couldn't arbitrarily build my table by 4 nodes and adding them.. I found out something very important.. if you print out a table entry that is a node or dimension to another table and not an entry that has a value i.e. print resources.colors <- a node rather than resources.colors.white.rgb <-which has been assigned a value then you a returned table: and a memory reference..
This behaviour could be changed by adding a ToString meta method to the table.

But meta tables and meta methods are undocumented in Hollywood because I think it's too complicated to understand for the average user. Consult the lua manual for insights into this topic. Hollywood has meta tables and methods fully implemented and they are very very powerful if you know how to use them :-)
I'm sorry for the long post but I hope somebody gets some use out of it and it helps them learn a little bit more about tables without having to go through as much trial and error over small things as I did.. I apologise for any inaccuracies listed above and would like to know of any corrections if my ramblings are a little bit misleading. If you have got something out of this let me know and I will continue to post tidbits when I find learn some more so that others may benefit from my blundering..
I think it is very helpful to post such messages. Tables are a bit tricky to understand at first, but if you know how they work, it's very easy to use them.
If this has already been discussed in more details or if there are references readily available that expands on what I have written I would also like to know as I'm sure I have only started at the tip of the iceberg with tables or even my understanding of the lua script language.
I did explain the table concept a little in the Hollywood documentation but you're of course using tables in a more advanced way with lots of dimensions and so on. Just remember that everything is dynamic in Hollywood. So you can really do stuff like

Code: Select all

my_table = {1,2,3,4,5,6,"String", "Another string", my_func = Function(x) DebugPrint(x) EndFunction}
Then you could do

Code: Select all

another_table = my_table
and access everything from the new table another_table, e.g.

Code: Select all

another_table.my_func(another_table[0])
etc.

It's so much fun to program Hollywood! ;-)
Locked