23 posts
Posted 04 July 2016 - 06:20 PM
I have a for loop designed to test if the user's input, matches an entry in a table. The for loop I used is shown here:
local list = { [2] = "value1", [6] = "value2"}
local foundvalue = false
local uservalue = io.read()
for key,value in pairs(list) do
if value == uservalue then
foundvalue = true
else
–nothing is supposed to go here
end
end
if foundvalue == false then–executes code at the end of the for loop if it has not found the value
–blah,blah,blah
the for loop works great, but only once. The next time I try to activate it from a function, it doesn't seem to want to run.
Any ideas?
Edited on 04 July 2016 - 04:25 PM
758 posts
Location
Budapest, Hungary
Posted 04 July 2016 - 07:14 PM
It'd be best if you posted the full code. The code above doesn't seem to be prone to errors.
Also, use [code][/code] tags.
print("They make things more readabe.")
Edited on 04 July 2016 - 05:16 PM
23 posts
Posted 04 July 2016 - 07:24 PM
hold on a sec. I think i found something
WOW! I've got to stop posting on the forums to early. I needed to put this:
local list = { [2] = "value1", [6] = "value2"}local foundvalue = falselocal uservalue = io.read()foundvalue = false
for key,value in pairs(list) doif value == uservalue thenfoundvalue = trueelse–nothing is supposed to go hereendendif foundvalue == false then–blah,blah,blah
it needed to set foundvalue back to false so that if it could run again. Sorry for the trouble.
Also by using
tags, does that put it into code form?
758 posts
Location
Budapest, Hungary
Posted 04 July 2016 - 07:26 PM
Yeah, that's something I couldn't have noticed. I couldn't tell whether foundvalue was local to the function or not. I guessed it was but, apparently, it wasn't.
Nice catch, though. Solving it yourself, that is.
23 posts
Posted 04 July 2016 - 07:30 PM
Yeah. That usually happens. I post and then find the problem
Thanks for trying to help me. Appreciate it :)/>
758 posts
Location
Budapest, Hungary
Posted 04 July 2016 - 07:38 PM
Oh, and yeah, code tags do that. Just don't copy the one I used for showing it; it's escaped.
[code]print("you type stuff here")[/code]
and you get
print("you type stuff here")
Unfortunately they cannot be used inline.
3057 posts
Location
United States of America
Posted 04 July 2016 - 08:18 PM
For inline code, set the font to monospace.
477 posts
Location
Germany
Posted 05 July 2016 - 04:29 PM
Also if you post intend your code
Edit: Actually always intend your code it will make it WAY easier for you to read
local list = { [2] = "value1", [6] = "value2"}
local foundvalue = false
local uservalue = io.read()
for key,value in pairs(list) do
if value == uservalue then
foundvalue = true
else
--nothing is supposed to go here
end
end
if foundvalue == false then--executes code at the end of the for loop if it has not found the value
--blah,blah,blah
vs.
local list = { [2] = "value1", [6] = "value2"}
local foundvalue = false
local uservalue = io.read()
for key,value in pairs(list) do
if value == uservalue then
foundvalue = true
else
--nothing is supposed to go here
end
end
if foundvalue == false then--executes code at the end of the for loop if it has not found the value
--blah,blah,blah
Edited on 05 July 2016 - 02:29 PM
239 posts
Location
Macintosh HD/Users/LoganDark
Posted 05 July 2016 - 08:21 PM
Also if you post intend your code
Edit: Actually always intend your code it will make it WAY easier for you to read
local list = { [2] = "value1", [6] = "value2"}
local foundvalue = false
local uservalue = io.read()
for key,value in pairs(list) do
if value == uservalue then
foundvalue = true
else
--nothing is supposed to go here
end
end
if foundvalue == false then--executes code at the end of the for loop if it has not found the value
--blah,blah,blah
vs.
local list = { [2] = "value1", [6] = "value2"}
local foundvalue = false
local uservalue = io.read()
for key,value in pairs(list) do
if value == uservalue then
foundvalue = true
else
--nothing is supposed to go here
end
end
if foundvalue == false then--executes code at the end of the for loop if it has not found the value
--blah,blah,blah
Indent*
Edit: Indented code probably looks like this:
local list = { [2] = "value1", [6] = "value2"}
local foundvalue = false
local uservalue = io.read()
for key,value in pairs(list) do
if value == uservalue then
foundvalue = true
else
--nothing is supposed to go here
end
end
if foundvalue == false then--executes code at the end of the for loop if it has not found the value
--blah,blah,blah
end -- before, you had a missing end
Not this:
local list = { [2] = "value1", [6] = "value2"}
local foundvalue = false
local uservalue = io.read()
for key,value in pairs(list) do
if value == uservalue then
foundvalue = true
else
--nothing is supposed to go here
end
end
if foundvalue == false then--executes code at the end of the for loop if it has not found the value
--blah,blah,blah
end -- before, you had a missing end
Edited on 05 July 2016 - 06:23 PM
477 posts
Location
Germany
Posted 06 July 2016 - 07:16 PM
-snip-
Indent*
-snap-
You know what I mean. I have no idea why the code is not indented for you. I actually did indent it.
Also if you post intend your code
Edit: Actually always intend your code it will make it WAY easier for you to read
local list = { [2] = "value1", [6] = "value2"}
-snip-
[CODE]
local list = { [2] = "value1", [6] = "value2"}
local foundvalue = false
local uservalue = io.read()
for key,value in pairs(list) do
if value == uservalue then
foundvalue = true
else
--nothing is supposed to go here
end
end
if foundvalue == false then--executes code at the end of the for loop if it has not found the value
--blah,blah,blah
239 posts
Location
Macintosh HD/Users/LoganDark
Posted 06 July 2016 - 07:17 PM
-snip-
Indent*
-snap-
You know what I mean. I have no idea why the code is not indented for you. I actually did indent it.
Also if you post intend your code
Edit: Actually always intend your code it will make it WAY easier for you to read
local list = { [2] = "value1", [6] = "value2"}
-snip-
[CODE]
local list = { [2] = "value1", [6] = "value2"}
local foundvalue = false
local uservalue = io.read()
for key,value in pairs(list) do
if value == uservalue then
foundvalue = true
else
--nothing is supposed to go here
end
end
if foundvalue == false then--executes code at the end of the for loop if it has not found the value
--blah,blah,blah
I'd imagine if you indented using tabs that they were removed.
477 posts
Location
Germany
Posted 06 July 2016 - 09:39 PM
-snip-
I'd imagine if you indented using tabs that they were removed.
I used two spaces to indent.