Set table based on user input??? [SOLVED]

Runes of Magic/Radiant Arcana (http://www.runesofmagic.com)
Post Reply
Message
Author
noobbotter
Posts: 527
Joined: Fri Aug 31, 2012 1:15 pm

Set table based on user input??? [SOLVED]

#1 Post by noobbotter » Tue Feb 11, 2014 8:46 am

I was wondering why this doesn't work:

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))
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

Code: Select all

if selected == "1" then
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? :oops:

User avatar
lisa
Posts: 8332
Joined: Tue Nov 09, 2010 11:46 pm
Location: Australia

Re: Set table based on user input??? [SOLVED]

#2 Post by lisa » Tue Feb 11, 2014 5:15 pm

io.stdin:read() returns the input as a string, correct ;)
Remember no matter you do in life to always have a little fun while you are at it ;)

wiki here http://www.solarstrike.net/wiki/index.php?title=Manual

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest