Ulysses
General => Developers Corner => Topic started by: MCFX2 on August 04, 2016, 05:27:25 PM
-
In lua (obviously) I am trying to make a table with the following structure (variable length):
local myTable = {
{"Alice", "1234"},
{"Bob","3242"}
}
Now say we have variable foo which is a string that could equal "alice", "bob" or neither. If foo="Alice" it should return "1234" and "bob" returns "3242", neither returns some predefined string like "ERROR".
How would I implement this?
-
You can use the syntax table[ key ] to access 'key' in 'table'. So, myTable[ foo ] is what you're looking for. If foo is set to a value that isn't a key in myTable then it will return nil. If you want to return something other than nil, you can use the __index metamethod (https://www.lua.org/pil/13.4.1.html).
-
Thanks a bunch! This seems to work. It's not the syntax I was hoping for, but it's good enough for me.