85

So, I have a table something along these lines:

arr =
{
  apples = { 'a', "red", 5 },
  oranges = { 'o', "orange", 12 },
  pears = { 'p', "green", 7 }
}

It doesn't seem like it's possible to access them based on their index, and the values themselves are tables, so I just made the first value of the nested table the index of it, so it now looks like this:

arr =
{
  apples = { 0, 'a', "red", 5 },
  oranges = { 1, 'o', "orange", 12 },
  pears = { 2, 'p', "green", 7 }
}

So, now any time I use one of these tables, I know what the index is, but still can't get to the table using the index, so I started to write a function that loops through them all, and check the indexes until it finds the right one. Then I realized... how can I loop through them if I can't already refer to them by their index? So, now I'm stuck. I really want to be able to type arr.apples vs arr[1] most of the time, but of course it's necessary to do both at times.

2
  • 1
    This with answer from @greatwolf will fail unless there is an "," between each array-row. arr = { apples = { 0, 'a', "red", 5 }, oranges = { 1, 'o', "orange", 12 }, pears = { 2, 'p', "green", 7 }, } a working example can be seen at: ideone.com/yWKHx7 Sep 4, 2015 at 12:08
  • @KjellHedström I've edited the OP's question and fixed up the minor syntax errors. good catch.
    – greatwolf
    Sep 8, 2015 at 20:17

5 Answers 5

145

To iterate over all the key-value pairs in a table you can use pairs:

for k, v in pairs(arr) do
  print(k, v[1], v[2], v[3])
end

outputs:

pears   2   p   green
apples  0   a   red
oranges 1   o   orange

Edit: Note that Lua doesn't guarantee any iteration order for the associative part of the table. If you want to access the items in a specific order, retrieve the keys from arr and sort it. Then access arr through the sorted keys:

local ordered_keys = {}

for k in pairs(arr) do
    table.insert(ordered_keys, k)
end

table.sort(ordered_keys)
for i = 1, #ordered_keys do
    local k, v = ordered_keys[i], arr[ ordered_keys[i] ]
    print(k, v[1], v[2], v[3])
end

outputs:

  apples  a   red     5
  oranges o   orange  12
  pears   p   green   7
9
  • Just noticed... why does it print them in the wrong order? If it printed them in order, I could leave out the index variable in the table, and just rely on the order that it prints to find the index, which would be nice. Jul 3, 2013 at 0:02
  • 4
    Lua doesn't guarantee the iteration order for the associative part of the table.
    – greatwolf
    Jul 3, 2013 at 0:34
  • 2
    @Lemony I've added an example on how you can traverse the table in a particular order.
    – greatwolf
    Jul 3, 2013 at 0:45
  • 1
    @HenrikErlandsson - A JSON object is unordered. See the spec.
    – beatgammit
    Jun 16, 2014 at 21:29
  • 2
    When Crockford writes "An object is an unordered set of name/value pairs" he means a JSON string represents an object with pairs whose order should not be relied upon. However JSON is a text interchange format, and what people call a "JSON object" is really just a string, so yes, it pairs are ordered. I think the term "JSON object" is overused, personally. Henrik is correct that the idea of someone wanting to iterate in the order given in the JSON string is a valid one, but a "wrong" one indeed!
    – Ray Toal
    Dec 19, 2014 at 16:38
9

If you want to refer to a nested table by multiple keys you can just assign them to separate keys. The tables are not duplicated, and still reference the same values.

arr = {}
apples = {'a', "red", 5 }
arr.apples = apples
arr[1] = apples

This code block lets you iterate through all the key-value pairs in a table (http://lua-users.org/wiki/TablesTutorial):

for k,v in pairs(t) do
 print(k,v)
end
8

For those wondering why ipairs doesn't print all the values of the table all the time, here's why (I would comment this, but I don't have enough good boy points).

The function ipairs only works on tables which have an element with the key 1. If there is an element with the key 1, ipairs will try to go as far as it can in a sequential order, 1 -> 2 -> 3 -> 4 etc until it cant find an element with a key that is the next in the sequence. The order of the elements does not matter.

Tables that do not meet those requirements will not work with ipairs, use pairs instead.

Examples:

ipairsCompatable = {"AAA", "BBB", "CCC"}
ipairsCompatable2 = {[1] = "DDD", [2] = "EEE", [3] = "FFF"}
ipairsCompatable3 = {[3] = "work", [2] = "does", [1] = "this"}

notIpairsCompatable = {[2] = "this", [3] = "does", [4] = "not"}
notIpairsCompatable2 = {[2] = "this", [5] = "doesn't", [24] = "either"}

ipairs will go as far as it can with it's iterations but won't iterate over any other element in the table.

kindofIpairsCompatable = {[2] = 2, ["cool"] = "bro", [1] = 1, [3] = 3, [5] = 5 }

When printing these tables, these are the outputs. I've also included pairs outputs for comparison.

ipairs + ipairsCompatable
1       AAA
2       BBB
3       CCC

ipairs + ipairsCompatable2
1       DDD
2       EEE
3       FFF

ipairs + ipairsCompatable3
1       this
2       does
3       work

ipairs + notIpairsCompatable

pairs + notIpairsCompatable
2       this
3       does
4       not

ipairs + notIpairsCompatable2

pairs + notIpairsCompatable2
2       this
5       doesnt
24      either

ipairs + kindofIpairsCompatable
1       1
2       2
3       3

pairs + kindofIpairsCompatable
1       1
2       2
3       3
5       5
cool    bro
2

All the answers here suggest to use ipairs but beware, it does not work all the time.

t = {[2] = 44, [4]=77, [6]=88}

--This for loop prints the table
for key,value in next,t,nil do 
  print(key,value) 
end

--This one does not print the table
for key,value in ipairs(t) do 
  print(key,value) 
end
4
  • Why does it not print all the time?
    – Zeruno
    Jan 3, 2020 at 0:29
  • I am beginner at Lua. I don't know. But I encountered this issue while testing my code.
    – sgowd
    Jan 3, 2020 at 4:41
  • @sgowd why are you adding ',nil' after the table 't' - it's not required. Also the issue mentioned by you (actually not an issue) is that ipairs returns a set of integer based keys. That means if your table has string-based keys, using ipairs you are converting them to integer keys. The docs describes the difference very clearly. Feb 25, 2020 at 10:59
  • 1
    @SeniruPasan Thanks for the tip on not adding the ',nil'; I will try it out. In the example I have given, the keys are integer based keys. Baggef's answer stackoverflow.com/a/60088452/1566608 gives more clarity on why ipairs doesn't work all the time.
    – sgowd
    Feb 25, 2020 at 22:33
-1

Hi guys i am new in LUA but these answers helped me only like an half.So i write my own.

for i in pairs(actions) do 
    if actions[i][3] ~= nil then
    --do something
    end
end
  1. i is index of value in table similar like in c#
  2. actions is just name of table
  3. actions[i][3] will check all indexes in table if their 3rd value is not nil

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.