0

I feel like this is a really silly question...

t = {
a = {x,y},
b = {z},
}

How do I print the first value of the key 'a'? ("x") Neither

print(t["a"][1]) 

nor

print(t.a[1])

does it, so how would I go about?

What's the difference between '[]' and '.' btw?

1
  • 2
    In Lua string literals must be enclosed in quotes: t = {a = {'x','y'}, b = {'z'}} Commented Apr 29, 2013 at 16:34

2 Answers 2

3

What's the difference between '[]' and '.' btw?

The dot is just a shortcut / syntax sugar for the index operator [] with strings. foo.bar is equivalent to foo["bar"]. foo[bar] on the other hand would return the value at the index of the value of the variable bar, so if bar happens to be baz it would do foo["baz"] / foo.baz.

You must use the []-syntax when using strings which are no valid Lua identifiers ("variable names") or non-strings as indices, like foo["Jon Doe"] or foo[3].

Sign up to request clarification or add additional context in comments.

Comments

1

In this code, x, y, and z are all undefined and so t.a and t.b are empty tables. My answer to your previous question stores x, y, and z as strings (but as keys, not as values).

1 Comment

and so it was a silly question. I got it to work, I thank you again!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.