Jump to content




for loop is only able to run once?


  • You cannot reply to this topic
11 replies to this topic

#1 Varscott11

  • Members
  • 23 posts

Posted 04 July 2016 - 05: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 by Varscott11, 04 July 2016 - 05:25 PM.


#2 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 04 July 2016 - 06: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 by LBPHacker, 04 July 2016 - 06:16 PM.


#3 Varscott11

  • Members
  • 23 posts

Posted 04 July 2016 - 06: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 = false
local uservalue = io.read()

foundvalue = false
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
--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 [code] tags, does that put it into code form?

#4 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 04 July 2016 - 06: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.

#5 Varscott11

  • Members
  • 23 posts

Posted 04 July 2016 - 06:30 PM

Yeah. That usually happens. I post and then find the problem

Thanks for trying to help me. Appreciate it :)

#6 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 04 July 2016 - 06: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.

#7 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 04 July 2016 - 07:18 PM

For inline code, set the font to monospace.

#8 Luca_S

  • Members
  • 407 posts
  • LocationGermany

Posted 05 July 2016 - 03: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 by Luca_S, 05 July 2016 - 03:29 PM.


#9 LoganDark

  • Members
  • 231 posts
  • LocationMacintosh HD/Users/LoganDark

Posted 05 July 2016 - 07:21 PM

Posted Image

View PostLuca_S, on 05 July 2016 - 03:29 PM, said:

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 by LoganDark, 05 July 2016 - 07:23 PM.


#10 Luca_S

  • Members
  • 407 posts
  • LocationGermany

Posted 06 July 2016 - 06:16 PM

View PostLoganDark, on 05 July 2016 - 07:21 PM, said:

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

View PostLuca_S, on 05 July 2016 - 03:29 PM, said:

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


#11 LoganDark

  • Members
  • 231 posts
  • LocationMacintosh HD/Users/LoganDark

Posted 06 July 2016 - 06:17 PM

View PostLuca_S, on 06 July 2016 - 06:16 PM, said:

View PostLoganDark, on 05 July 2016 - 07:21 PM, said:

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

View PostLuca_S, on 05 July 2016 - 03:29 PM, said:

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.

#12 Luca_S

  • Members
  • 407 posts
  • LocationGermany

Posted 06 July 2016 - 08:39 PM

View PostLoganDark, on 06 July 2016 - 06:17 PM, said:

-snip-
I'd imagine if you indented using tabs that they were removed.
I used two spaces to indent.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users