12

How can I get the data which is a table inside a table, i mean like this:

t = { {a, b, c}, {d, e, f} };

if I write this line of code:

print( t[1] )

the result will be —–>>> {a, b, c}

BUT

how can I print just the letter “a”? without using ipairs I mean is there any way to use something like t[1]?

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Ali
  • 131
  • 1
  • 1
  • 4

1 Answers1

13

Have you tried t[1][1]? That should get you the first index in the table you get from t[1]

Alex
  • 14,973
  • 13
  • 59
  • 94
  • 4
    Are you trying to print out the character 'a' or the value of the variable a? In the example you gave, a, b, c, d, e and f are variables, not characters, and if they haven't been initialized they'll be nil. If you want to print the character 'a' change your code to be `t = { {"a", "b", "c"}, {"d", "e", "f"} };` – Alex Jul 24 '11 at 16:29
  • 1
    Thank you very much Alex, appreciate your help. you'r right, you saved my life. But because you are very helpful, I want to ask you another question :). if I have another table which contains just numbers like this: table = { {1,2,3}, {4, 5, 6} } how can I print 3? do we consider it as a variable too? Thank you very very much, – Ali Jul 24 '11 at 16:52
  • 2
    No, the number 3 would not be a variable, it's simply a number. Variables are things you can set to different values (and different types). If you wanted to print 3 from that table, you would type `print(t[1][3])`, which gets third index from the first table. If you have not already, I suggest you read the online book ["Programming in Lua"](http://www.lua.org/pil/). The stuff you're asking about is mostly covered in the first two chapters. The book is fairly short, it shouldn't take more than a weekend to read through most of it. – Alex Jul 24 '11 at 17:01
  • 2
    Alex, You Are The Best. Thank you – Ali Jul 24 '11 at 17:14