1
    arr = {
       {'a',1},
       {'b',2},
       {'c',3}
}

    print(arr[1][1])
    print(arr[2][2])
    print(arr[2])

output would be

a 2 table: 0x1e41080, This may be a very newbie question for you guys. But even though it is really embarrassing to ask such question here, I ask because couldn't find solution in online. Is there a way that can point whole table of table? Like I want to point {'b',2} instead of the address of array/table.

GothLoli
  • 19
  • 1
  • 6

2 Answers2

2

It's not clear what you call by "point 2d array", as there's no pointers in Lua.

There's also no 2d arrays in Lua, there's tables of tables.

What you get with arr[2] is exactly one value of inner table that you can pass around. It's not "just address", it's a table that you can read/write anywhere later. You see something that look as an address because that's default behavior for print for tables.

local arr = {
       {'a',1},
       {'b',2},
       {'c',3}
}

local function display(v)
    print(v[1], v[2])
end

display(arr[1])
display(arr[2])
display(arr[3])

Note that you can't have pointer (a reference) to some element within the table. You need two values to address individual elements of a table - a table itself and the value of its index (which is not necessarily a number or a text).

Vlad
  • 5,450
  • 1
  • 12
  • 19
1

There's no built-in way to do it. If your tables all have the same structure, you can use this to get a string:

('{%q,%d}'):format(element[1], element[2])

If your tables are more complex, you can use a loop with more complex code.

luther
  • 5,195
  • 1
  • 14
  • 24