PDA

View Full Version : How to use nested tables


sdfsf93924554234fjfjf
01-27-2008, 07:09 PM
hi,I'm working on an app at the moment that is bringing in database data. To use that data in MS I want to convert the raw table into a nested table. Ideally, this would be a 3 level deep nested table (array) with various types of info stored in it. Some info would just need the 1 or 2 levels, whilst others would need all 3. i.e.

order[1][1][1]

would store...

order.items.item1.item Name

or

order.items.item2.item2 quantity

or

order.InvoiceNumber

or

order.deliveryType

Problem is... I can't grasp from the docs how you exactly create a dynamic nested array of this kind. Is this even possible? In other scripting languages I would expect to define the original array (order[][][]), and then to be freely able to add values to it ad-hoc, and for the table to adjust as needed. But what I actually find is that assigning say...

order[1][1] = "funky";

works. Whilst..

order[13][1] = "bummer";

does not....

If nested tables in LUA are not dynamic, it looks like I'll just have to forget the whole nested system and go for a much flatter (and harder to code model).

And guidance appreciated here. Does anyone have any extensive examples of how to create and work with nested arrays? Can they be fully dynamic i.e. can I simply assign values to any level of the nest and LUA will create the tables as needed on the fly?

Cheers

Chris

reteset
01-28-2008, 10:07 AM
i never tried it out.
just a suggestion.
i don't know does it work.

order = {sub1={},sub2={},sub3={}};

order.sub1[1] = "any";
order.sub1[2] = "any2";

order.sub2[1] = "any";
order.sub2[2] = "any2";

order.sub2[1] = "any";
order.sub2[2] = "any2";

Or

order = {sub1={sub2={sub3={}}}};

order[1].sub1[1].sub2[1].sub3[1] = "any";

TJ_Tigger
01-29-2008, 11:39 AM
Yeah that can be a tricky subject to start working with. Once you have it you will be able to work with it relatively easy. One thing that might help in seeing how nested tables work is to look at the SQLite.QueryToTable function and see how it returns information and how you can access the different points of data.

You can define a table with the following code

mytable = {};

Now that you have a table you can then populate that table with strings, numbers, tables, functions and all sorts of stuff. So how do we do this?

Once your table is created you can then populate it with the following.

mytable.version = "1.0.0.1";

This adds an entry to the table called 'version' that would return 1.0.0.1 when called.

Dialog.Message("table info", mytable.version);

Defining a table within a table is just like defining a table.

mytable.nestedtable = {};

Now the value of mytable.nestedtable is a table. You could use a for loop to dynamically populate it as well

for n = 1,10 do
mytable.nestedtable[n] = "value"..n;
end

And you could then read out the table like this

for index, value in mytable.nestedtable do
Dialog.Message(index,value);
end

or you could return a specific value like this

Dialog.Message("number 4", mytable.nestedtable.4);

or

Dialog.Message("number 4", mytable.nestedtable[4]);

If your array uses a named field make sure to quote the named field within the square brackets.

mytable.nestedtable.namedvalue = "mysoftware"

Dialog.Message("My softwares name", mytable.nestedtable["namedvalue"]);

Hopefully that will help to get you started anyway. My disclaimer on the above code is as follows. I didn't type it in AMS so I was pulling it off the top of my head. Hopefully it will work if you were to cut and paste but I might have spelling errors or syntatical errors. But hopefully it will help you learn the ropes.

Tigg

sdfsf93924554234fjfjf
01-31-2008, 07:36 PM
Thanks for the run through Tiggs. I now have a working system in place, it does not use nested tables to the degree I had planned. But I have used them to a smaller degree.

Cheers

CT