I am trying to write a lua switch, based on what I have read so far it seems this is achieved by using a table. So I made a really barebones table but when I try to run it I get an error saying table index is null.
Ultimately what I want is based on different input, this code should call on different lua files. But for now since I am new to this language I figured I would settle for not having that index error.
thanks
#!/usr/bin/lua
-- hello world lua program
print ("Hello World!")
io.write('Hello, where would you like to go?\n')
s = io.read()
io.write('you want to go to ',s,'\n')
--here if i input a i get error
location = {
[a] = '1',
[b] = '2',
[c] = '3',
[d] = '4',
}
location[s]()
Above is the code that I got so far. Below is the error.
~$ lua lua_hello.lua
Hello World!
Hello, where would you like to go?
a
you want to go to a
lua: lua_hello.lua:11: table index is nil
stack traceback:
lua_hello.lua:11: in main chunk
[C]: in ?
The table code is based on this example here: Lua Tables Tutorial section:Tables as arrays