Announcement

Collapse
No announcement yet.

Associative arrays - how to?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Associative arrays - how to?

    I've created two arrays:

    Code:
    -- first array (indexed by numbers 1...4)
    
    array_numeric = {"Data1", "Data2", "Data3", "Data4"};
    
    -- second array (indexed by labels Item1...Item4)
    
    array_associative = {Item1 = "Data1", Item2 = "Data2", Item3 = "Data3", Item4 = "Data4"};
    I need to get string values from "Data2" to "Data4".

    For first array it will be like:

    Code:
    array_new = {};
    for c = 2, Table.Count(array_numeric) do

  • #2
    Code:
    array_new = {};
    for c = 2, Table.Count(array_numeric) do
          array_new[c - 1] = array_numeric[c];
    end
    How to make the same operation with array_associative?

    I know only this loop:

    Code:
    array_new = {};
    for k,v in pairs (array_associative) do
          array_new[k] = v;
    end
    But how to start at Item2?

    Thanks

    Comment


    • #3
      for can be used another way:

      Code:
      for i = 2, 100 do
       -- Starts at 2
       -- Ends at 100
      end

      Comment


      • #4
        Shrek
        It is not for associative arrays. Your example is the same what I posted for number indexed arrrays.

        Comment


        • #5
          Its for looping through something starting at a given value and ending at a given value, that is what you asked?

          In the loop you access the table you already created with i being the table reference point.

          Comment


          • #6
            I'll try another way to explain what I mean.

            For example you have associative array named "table". You don't know index-keys of this array. You don't know number of index-keys of this array. The order of keys is no matter for you. You have to get values from array one by one with pause between sampling. You have to get next value (NOT LOOP FROM FIRST VALUE, because a lot of time will be lost) each time. How to create this loop?

            Comment


            • #7
              A loop will loop from x to y or break and there is no getting around that, if looping would be taking a long time then directly access what you want instead.

              You are creating the table so you do now the index/key values.

              Comment


              • #8
                Shrek
                My English not good, sorry for that.

                Can you post an example of code?

                You have array:

                Code:
                table = {
                UnknownUniqueID1 = "SomeData1",
                UnknownUniqueID2 = "SomeData2",
                UnknownUniqueID3 = "SomeData3",
                UnknownUniqueID4 = "SomeData4"
                };
                The real order of data you can find out like this:

                Code:
                text = "";
                c = 0;
                for k,v in pairs(table) do
                      c = c + 1;
                      text = text..c..": [key:"..k.."] = "..tostring(v).."\r\n";
                end
                Dialog.Message("Array Real Order", text, 0, 0, 0);
                You will get text:

                1: [key:UnknownUniqueID2] = SomeData2
                2: [key:UnknownUniqueID4] = SomeData4
                3: [key:UnknownUniqueID3] = SomeData3
                4: [key:UnknownUniqueID1] = SomeData1

                Please, post example of code that make loop that access array from UnknownUniqueID4 to UnknownUniqueID1.

                Comment


                • #9
                  You should note that an associative table works differently than a numerically indexed table.
                  You don't know index-keys of this array. You don't know number of index-keys of this array.
                  In this case, when you don't know the keys, then talking about a "second" item in the table may be misleading. From the documentation:
                  (...) internally the scripting engine doesn't store tables as arrays, but in a super-efficient structure known as a hash table. (...) The important thing to know is that when you define table elements, they are not necessarily stored in the order that you define or add them, unless you use a numeric array (i.e. a table indexed with numbers from 1 to whatever).
                  If you use an associative table, the "second" item may be internally stored as the first, the last, or anything in between. The same goes for the first item you inserted, it may appear at a different position once you walk the table using pairs().

                  If you just want a copy of the table with one less elements than the source, then use a for loop, and a counter. If you want to make sure that you indeed discard the first element you inserted, then create a numeric table instead, so you have an known index, and the second field of the table can be a table itself, with just one element. In other words, you could construct it with something like this:
                  Code:
                  Table.Insert(tMyTable, nPos, {Index="something"});
                  Ulrich

                  Comment


                  • #10
                    Ok, I understand. Only one solution is to use additional numeric indexed table and additional pointer variable to do this loop. I was hoping that there is a way to use some lua functions that makes it more easy without additional tables and variables.

                    Comment


                    • #11
                      good Ulrich :yes

                      Comment

                      Working...
                      X