Indigo Rose Software

Professional Software Development Tools

 
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2005
    Posts
    187

    Associative table index using a variable?

    Hi Everyone,

    I'm sure this has been addressed before, but I couldn't find it searching through the forums. How can I create a table index using a variable? Here's an example:
    Code:
    sIndex = "Anderson";
    sValue = "Jason";
    
    arrNames.sIndex = sValue; or arrNames = {sIndex=sValue};
    will result in "arrNames.sIndex" rather than "arrNames.Anderson"

    I thought I could get away with Table.Insert, but it only works for numeric arrays.

    How can I dynamically create an associative array using variables?

  2. #2
    Join Date
    Oct 2004
    Location
    East, South & West Asia
    Posts
    1,020
    Hi,

    A few ways to define table arrays.

    METHOD 1:

    Code:
    arrNames = {anderson="johnson"};
    
    --therefore making it into a variable:
    
    sName = arrNames.anderson;
    resultant : sName = johnson


    METHOD 2:

    Code:
    arrNames = {};
    arrNames[1] = {anderson="johnson"};
    
    --- therefore making it into a variable:
    
    sName = arrNames[1];
    resultant : sName = johnson

    --------------- so if I choose METHOD 1----------------
    Code:
    --define variables
    
    sIndex = "anderson";
    sValue = "johnson";
    
    arrNames = { sIndex = sValue }
    
    -- making it into another variable
    
    sName = arrNames.sIndex;
    resultant : sName = johnson
    Newbie Examples
    ------> AMS 7.5 : amstudio.azman.info
    ----> AMS 6 & 5: www.azman.info/ams/
    ----> FB: facebook.com/GuideToWealth

    ----> Content Development Blog: www.AZMAN.asia

  3. #3
    Join Date
    Feb 2001
    Location
    Indigo Rose Software
    Posts
    2,728
    You can also do

    Code:
    sIndex = "anderson";
    sValue = "johnson";
    
    arrNames[sIndex] = sValue;
    --[[ Indigo Rose Software Developer ]]

  4. #4
    Join Date
    Mar 2005
    Posts
    187
    Thanks Azmanar,

    Here's the problem I'm running into though. Working off your examples (method1 in this case), when I do this:

    Code:
    for index, value in arrNames do
         Dialog.Message("", "Index is: "..index);
         Dialog.Message("", "Value is: "..value);
    end
    the value is fine, but I'll get "Index is: sIndex" instead of "Index is: Anderson". Does this make sense?

  5. #5
    Join Date
    Mar 2005
    Posts
    187
    Quote Originally Posted by Lorne View Post
    You can also do

    Code:
    sIndex = "anderson";
    sValue = "johnson";
    
    arrNames[sIndex] = sValue;
    Ahhh Thanks Lorne, that worked. I thought []'s were for numeric arrays only, but it works for associative arrays too! Yay!

  6. #6
    Join Date
    Oct 2004
    Location
    East, South & West Asia
    Posts
    1,020
    Sorry. I was wrong about METHOD 2.

    METHOD 2:

    Should be:

    arrNames = {};
    arrNames[1] = {"johnson"};
    sName = arrNames[1];

    RESULTANT : sName = johnson

    and if I want to nest it further, it should be

    Should be:

    arrNames = {};
    arrNames[1] = {anderson = "johnson"};
    sName = arrNames[1].anderson;

    RESULTANT : sName = johnson


    Attached is an APZ for your reference.
    Attached Files
    Newbie Examples
    ------> AMS 7.5 : amstudio.azman.info
    ----> AMS 6 & 5: www.azman.info/ams/
    ----> FB: facebook.com/GuideToWealth

    ----> Content Development Blog: www.AZMAN.asia

Similar Threads

  1. prob getting table to combobox
    By Jonas DK in forum AutoPlay Media Studio 6.0
    Replies: 2
    Last Post: 01-05-2007, 04:56 PM
  2. How do I address a table whose name I know only as a variable?
    By JimS in forum AutoPlay Media Studio 5.0
    Replies: 6
    Last Post: 08-24-2005, 07:37 AM
  3. string / table compare
    By gabrielfenwich in forum AutoPlay Media Studio 5.0
    Replies: 5
    Last Post: 01-29-2005, 11:30 PM
  4. Associative Table Question
    By Worm in forum AutoPlay Media Studio 5.0
    Replies: 4
    Last Post: 12-09-2003, 06:43 PM
  5. Creating a Table of Contents
    By Desmond in forum AutoPlay Media Studio 5.0 Examples
    Replies: 0
    Last Post: 10-03-2003, 11:35 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