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

for loop is only able to run once?

Started by Varscott11, 04 July 2016 - 04:20 PM
Varscott11 #1
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
LBPHacker #2
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
Varscott11 #3
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 = 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
 tags, does that put it into code form?
LBPHacker #4
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.
Varscott11 #5
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 :)/>
LBPHacker #6
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.
KingofGamesYami #7
Posted 04 July 2016 - 08:18 PM
For inline code, set the font to monospace.
Luca_S #8
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
LoganDark #9
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
Luca_S #10
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
LoganDark #11
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.
Luca_S #12
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.