This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
sleawnis's profile picture

Faster Way Than Lts Of Elseif Statements

Started by sleawnis, 25 August 2013 - 12:18 PM
sleawnis #1
Posted 25 August 2013 - 02:18 PM
I am trying to make a simple program to display item id's for minecraft, so far:

write("Item Name:")
input = read()

As you can see I haven't got far without getting to a problem,

To display the I'd, the only way I can think is:

If input == "stone" then
print("I'd = 1")
elseif input == "grass" then
print("I'd = 2")
elseif…..

As you can see it will take a while to write all these out, is there a shorter way? Thanks!
Bubba #2
Posted 25 August 2013 - 02:33 PM
Tables are typically the best way to do this. I'd make a table like this:

local ids = {
  stone=1, cobblestone=4, dirt=2
}

etc. etc.

To access those values, you can use the indexes (stone, cobblestone, dirt)

input = read()
if ids[input] then
  print(input.." = "..ids[input])
else
  print("That item does not exist")
end

You can read more on tables here: http://lua-users.org/wiki/TablesTutorial