PDA

View Full Version : How to iteratively create tables



sdfsf93924554234fjfjf
03-18-2008, 05:29 PM
Ok, I'm trying to match the output (nested table)of a command in the luaSQL extension. This returns a number of lines from a query, and each line has a number of records. So for instance a query returns a table with 10 lines. line 1 can have CUSTOMERNAME, ADDRESS, AGE, DOGCOLOUR etc

So I need to create a numerical table, with nested associative arrays in it. To be honest the AMS docs fall far far short in this area explaining anything this complex. The code below works....

recy ={};
recy[1] = {john="1",jack="2",dave="3"};
Debug.Print(recy[1]["dave"]);

Prints

>> 3

So.. with that working as planned, I tried to build the required associative array using a for loop and assignments based on the loop variables....


for index,item in records do
myTable[1] = {item=index};
end
Debug.Print(myTable[1]["item"]);

And this create an array in myTable[1], with one element called "item", and its value is "index". What I expected was a table with as many elements as were in the table "records", and each elements key to be the value stored in "record" for that iteration, and for the value stored to be the current for/loops "index" value.

So, how can I build an associative table within a for loop?

Cheers

Chris Thomas

rexzooly
03-18-2008, 05:40 PM
Ok, I'm trying to match the output (nested table)of a command in the luaSQL extension. This returns a number of lines from a query, and each line has a number of records. So for instance a query returns a table with 10 lines. line 1 can have CUSTOMERNAME, ADDRESS, AGE, DOGCOLOUR etc

So I need to create a numerical table, with nested associative arrays in it. To be honest the AMS docs fall far far short in this area explaining anything this complex. The code below works....

recy ={};
recy[1] = {john="1",jack="2",dave="3"};
Debug.Print(recy[1]["dave"]);

Prints

>> 3

So.. with that working as planned, I tried to build the required associative array using a for loop and assignments based on the loop variables....


for index,item in records do
myTable[1] = {item=index};
end
Debug.Print(myTable[1]["item"]);

And this create an array in myTable[1], with one element called "item", and its value is "index". What I expected was a table with as many elements as were in the table "records", and each elements key to be the value stored in "record" for that iteration, and for the value stored to be the current for/loops "index" value.

So, how can I build an associative table within a for loop?

Cheers

Chris Thomas


Please don't cross post feller it kinda makes things amess.

sdfsf93924554234fjfjf
03-18-2008, 06:52 PM
K, just lookging for some help.....

rexzooly
03-18-2008, 07:05 PM
K, just lookging for some help.....

We all look for help and i know how it feels when people don't reply
but its just cos a they don't know or b they might be very busy if someone
can help you will get a reply.


:yes

sdfsf93924554234fjfjf
03-18-2008, 07:06 PM
Ok, I'm partially there, but still having problems

This...

for index,item in records do
myTable["" .. item .. ""] = "" .. index .. "";
end

iteratively builds the myTable table, and works as needed. What I cannot get working now is creating that table as a child of a numerical table value i.e.


for index,item in records do
myTable[1]["" .. item .. ""] = "" .. index .. "";
end

Fails..

Any ideas? This issue (tables and their construction/syntax) is a real thorn in my side with LUA. In other scripting languages its pretty clear how you can created nested arrays. In LUA it really seems to be poorly documented. I KNOW it can be done, as I have functions in the luaSQL extension that return nested arrays of quite complex types i.e. associative arrays nested inside numerical ones. But in all the docs I've found, this is very sketchy area.

CT



CT

sdfsf93924554234fjfjf
03-18-2008, 08:18 PM
Ok, I have a solution....

First, create a parent table, and then populate its numerical indicies with place-holder empty arrays....

itemsCount = Table.Count(initialSplit);
myTable = {};
for ti = 1, itemsCount do
myTable[ti] = {};
end

This basically builds us the table below, but in an iterative fashion....

myTable = {{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{ },{},{}};

Then we can populate each of these indicies with associative arrays as needed. i.e.

for index, Item in table do
myTable[1]["" .. key[index] .. ""] = "" .. value[index] .. "";
end

You can then access each nested database record as needed i.e. for record1, customers first name

name = myTable[1].FirstName


I should now be able to polish this into a lua function, which when paired with a PHP script I have, will give you a lua/PHP based mySQL query.

Cheers

CT