Indigo Rose Software

Professional Software Development Tools

 
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2007
    Location
    London, UK
    Posts
    19

    How to iteratively create tables

    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

  2. #2
    Join Date
    Jul 2007
    Posts
    1,512
    Quote Originally Posted by sdfsf93924554234fjfjf View Post
    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.

  3. #3
    Join Date
    Mar 2007
    Location
    London, UK
    Posts
    19
    K, just lookging for some help.....

  4. #4
    Join Date
    Jul 2007
    Posts
    1,512
    Quote Originally Posted by sdfsf93924554234fjfjf View Post
    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.



  5. #5
    Join Date
    Mar 2007
    Location
    London, UK
    Posts
    19
    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
    Last edited by sdfsf93924554234fjfjf; 03-18-2008 at 06:10 PM.

  6. #6
    Join Date
    Mar 2007
    Location
    London, UK
    Posts
    19
    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

Similar Threads

  1. Create batabase tables on the fly
    By RizlaUK in forum AutoPlay Media Studio 6.0
    Replies: 3
    Last Post: 01-13-2007, 03:31 PM
  2. Using a for loop to create multiple tables
    By TJ_Tigger in forum AutoPlay Media Studio 5.0
    Replies: 4
    Last Post: 04-20-2004, 07:30 AM
  3. HOWTO: Create a Project Template
    By Support in forum AutoPlay Media Studio 4.0 Examples
    Replies: 0
    Last Post: 10-28-2002, 01:49 PM
  4. HOWTO: Create a Page Template
    By Support in forum AutoPlay Media Studio 4.0 Examples
    Replies: 0
    Last Post: 10-26-2002, 05:20 AM
  5. HOWTO: Create Nested Shortcuts in the Start Menu
    By Support in forum Setup Factory 6.0 Knowledge Base
    Replies: 0
    Last Post: 09-24-2002, 10:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts