Lua - Getting values ​​from nested tables

Ok, so I searched everywhere for this, but no answer.

I have a nested table (example):

{
  {
    "Username",
    "Password",
    "Balance",
  },
  {
    "username1",
    "password1",
    1000000,
  },
  {
    "username2",
    "password2",
    1000000,
  },
}

The fact is that I can not iterate through the loop to view these tables and not get the values ​​from the tables. None of the nested tables can be easily accessed:

print(a[1])

How do I loop them and get values ​​from them?

+4
source share
2 answers

Use pairsor ipairsto iterate over the table:

local t = {
  {
    "Username",
    "Password",
    "Balance",
  },
  {
    "username1",
    "password1",
    1000000,
  },
  {
    "username2",
    "password2",
    1000000,
  },
}

for _, v in ipairs(t) do
  print(v[1], v[2],v[3])
end

will print:

Username    Password    Balance
username1   password1   1000000
username2   password2   1000000
+4
source

If you

a =  {
   { "Username", "Password", "Balance", },
   { "username1", "password1", 1000000, },
   { "username2", "password2", 1000000, },
}

a a[2], { "username1", "password1", 1000000, }. hyou , table: 0x872690 - , Lua . . a[2][1], a[2][2] ..

 for i = 2, #a do
     print(a[i][1], a[i][2], a[i][3])
 end
+3

Source: https://habr.com/ru/post/1547338/


All Articles