Associative table index using a variable?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • Buffman
    Indigo Rose Customer
    • Mar 2005
    • 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; [I]or[/I] 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?
  • azmanar
    Indigo Rose Customer
    • Oct 2004
    • 1020

    #2
    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

    Comment

    • Lorne
      Indigo Rose Staff Member
      • Feb 2001
      • 2729

      #3
      You can also do

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

      Comment

      • Buffman
        Indigo Rose Customer
        • Mar 2005
        • 187

        #4
        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?

        Comment

        • Buffman
          Indigo Rose Customer
          • Mar 2005
          • 187

          #5
          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!

          Comment

          • azmanar
            Indigo Rose Customer
            • Oct 2004
            • 1020

            #6
            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

            Comment

            Working...
            X