Set table based on user input??? [SOLVED]
Posted: Tue Feb 11, 2014 8:46 am
I was wondering why this doesn't work:
I was going to have something like this in one of my waypoint files for buying belts and fusions so that when I run it, I can choose the group I want to buy for and send to instead of having multiple copies of the same file with different names in the table. When I run it, it asks me for my input, then it prints the input properly (just for testing to verify it got my input properly, If I press 2, it prints 2 so I know it got my input correct). But then it gives me an error "bad argument #1 to 'maxn' (table expected, got nil)" I know that error means that it's not seeing "nameTable" as a table but I don't understand why.
** EDIT:
Well, I just figured it out. Apparently I have to put the number in quotes like
I guess it's saving the input as text and not as a number. I guess that makes sense. If the input was "y" or "n", those wouldn't be a number, would they? 
Code: Select all
print("Here are the options:")
print(" 1. Group 1")
print(" 2. Group 2")
print(" 3. Group 3")
printf("Enter Group to send belts to > ")
local selected = io.stdin:read()
printf("selected = %s\n",selected) -- this prints the number correctly
if selected == 1 then
nameTable = {
"Char1",
"Char2",
"Char3",
"Char4"
}
elseif selected == 2 then
nameTable = {
"Char17",
"Char18",
"Char19",
"Char20"
}
elseif selected == 3 then
nameTable = {
"Char33",
"Char34",
"Char35",
"Char36"
}
end
printf("table has %s items.\n",table.maxn(nameTable))
** EDIT:
Well, I just figured it out. Apparently I have to put the number in quotes like
Code: Select all
if selected == "1" then
