#1:
Code: Select all
for index, value in ipairs({42, 20, 6}) do
print(value)
end
index and
value can be any two names you want. A common idiom in Lua is to use the name
_ for values which won't be used.
Also, you if you call a function with a single literal table like in the code above, you can remove the opening and closing parentheses, if you find that easier to read.
Code: Select all
for _, x in ipairs{42, 20, 6} do
print(x)
end
#2:
S[something] works for strings as well as integers. Be sure to use quotes when using strings. Using the
. is simply "syntactic sugar". For example...
Code: Select all
S = {}
S["Length"] = 1000 -- Notice the quotes! Also, because this is preceded by "--", this is a comment.
print( S.Length ) -- This will output "1000".
print( S["Length"] ) -- This will also output "1000". This line is exactly equivalent to the above line.
You might want to check out some
Lua tutorials. EDIT: Like Qcode said below this post, this forum is for all questions. I hope this doesn't seem like "read tutorials instead of asking questions!", I think asking questions is perhaps the best way to learn, I just wanted to share some resources which might be able to answer your questions quicker. Just... just thought I'd say this, just in case.
Hope this helps!
EDIT: OOPS!
Thanks again Robin, sorry about that, I will try to take more care with my answers.