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={}
So you do this:
Code: Select all
v1$="colors"
v2$="white"
resources={}
resources[(v1$)]={}
resources[(v1$)][(v2$)]={}
Code: Select all
resources.colors[(v2$)]={}
or
resources[(v1$)].white={}
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>
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"
i.e.
Code: Select all
tags={"colors", "white", "rgb"}
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"
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'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