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

Breaking Out Of Nested Loops

Started by CCJJSax, 28 September 2013 - 11:51 PM
CCJJSax #1
Posted 29 September 2013 - 01:51 AM
I tried doing this, but either I'm doing it wrong, or Computercraft doesn't support this. This is really early code. So it really doesn't do much… or anything right now. But I'm trying to break out of the "terminate ()" function. How can I break 2 loops?

Spoilererror code: BIOS: 337: … 81: unexpected symbol


validSlotNumber = {}
for i = 1, 16 do
  validSlotNumber[ i ] = true
end

if numb == nil then
  while true do
   evt, pl = os.pullEventRaw("key")
   if evt == "terminate" then
	( local function terminate() -- this is line 81
	 print("are you sure? Y/N")
	 while true do
	  local evt, pl = os.pullEventRaw()
	  if pl == "y" or "Y" then
	   return
	  elseif pl == "n" or "N" then
	   print("ok")
	   break
	  end
	end )
  
   end
  end
end
MKlegoman357 #2
Posted 29 September 2013 - 02:35 AM
Tip: Next time show us the full error message you get. It helps us to help you.

You don't need "local" and "terminate" when making a function that way. You also need to call your function using another pair of paranthesis:


(function ()
  print("test")
end)()--//Here
CCJJSax #3
Posted 29 September 2013 - 02:42 AM
Tip: Next time show us the full error message you get. It helps us to help you.

You don't need "local" and "terminate" when making a function that way. You also need to call your function using another pair of paranthesis:


(function ()
  print("test")
end)()--//Here

Oops, I meant to add in the error message. Thanks for the heads up.

Your way looks way easier than the way I was using :)/>
CCJJSax #4
Posted 29 September 2013 - 11:33 PM
Hmmm. I guess I'm not understanding what you're suggesting as well as I thought.



while true do
print("loop 1")
sleep(1)
( function()
  while true do
   print("loop 2")
   sleep(1)
  end
end) ()
end
print('done')


error code : [string "temp"]:7: ambiguous syntax (function call x new statement)
campicus #5
Posted 30 September 2013 - 12:10 AM
This would be the correct format, if I am understanding you correctly.


function function()
  print("do something")
end

while true do
  print("loop 1")
  sleep(1)
  function() --you had an added paranthesis here
  while true do
	print("loop 2")
	sleep(1)
  end
end --not sure what the parentheses here were doing
-- there was an extra 'end' here
print("done") --use the correct quotations
MKlegoman357 #6
Posted 30 September 2013 - 06:51 AM

print("done") --use the correct quotations


print('Hi')

--//Same as:

print("Hi")

--//Same as:

print([[Hi]])

OP:
What you should really do is call your while loops in a function:


local function loop ()
  while true do --//First while loop
    local e, p = os.pullEventRaw("key")

    if e == "terminate" then
      while true do --//Second while loop
        print("Are you sure you want to terminate? [y/N]")

        local _, k = os.pullEventRaw("key")

        if k == keys.y then
          return --//Will exit the function
        elseif k == keys.n then
          break --//Will break the second while loop
        elseif k == keys.enter or k == keys.numPadEnter then
          break --//Will break the second while loop
        end
      end
    end
  end
end

loop()
print("Done")
Edited on 30 September 2013 - 04:59 AM
AgentE382 #7
Posted 30 September 2013 - 07:25 PM
Fixed way:
Spoiler
validSlotNumber = {}
for i = 1, 16 do
  validSlotNumber[ i ] = true
end

if numb == nil then
  while true do
    evt, pl = os.pullEventRaw("key")
    if evt == "terminate" then
      (function() -- "AgentE382: You must define an anonymous global function if you want to immediately call it."
        print("are you sure? Y/N")
        while true do
          local evt, pl = os.pullEventRaw()
          if pl == "y" or pl == "Y" then -- "AgentE382: You were missing the `pl ==` after `or`."
            return
          elseif pl == "n" or pl == "N" then -- "AgentE382: You were missing the `pl ==` after `or`."
            print("ok")
            break
          end
        end
      end)()  -- "AgentE382: You were missing an `end` statement."
    end
  end
end
Best way:
Spoiler
validSlotNumber = {}
for i = 1, 16 do
  validSlotNumber[ i ] = true
end

if numb == nil then
  while true do
    evt, pl = os.pullEventRaw("key")
    if evt == "terminate" then
      print("are you sure? Y/N")
      while true do
        local evt, pl = os.pullEventRaw()
        if pl == "y" or pl == "Y" then -- "AgentE382: You were missing the `pl ==` after `or`."
          return
        elseif pl == "n" or pl == "N" then -- "AgentE382: You were missing the `pl ==` after `or`."
          print("ok")
          break
        end
      end
    end
  end
end
Note: I'm using quotation marks to highlight my comments because of the broken syntax highlighting this forum uses.

EDIT: do..end blocks don't work like I thought they did. In this specific instance, using a `return` statement without anything else works just fine. I've updated my code snippet. Test it if you don't believe me.
Edited on 02 October 2013 - 07:50 PM
MKlegoman357 #8
Posted 01 October 2013 - 06:19 AM
Fixed way:
Spoiler
validSlotNumber = {}
for i = 1, 16 do
  validSlotNumber[ i ] = true
end

if numb == nil then
  while true do
	evt, pl = os.pullEventRaw("key")
	if evt == "terminate" then
	  (function() -- "AgentE382: You must define an anonymous global function if you want to immediately call it."
		print("are you sure? Y/N")
		while true do
		  local evt, pl = os.pullEventRaw()
		  if pl == "y" or "Y" then
			return
		  elseif pl == "n" or "N" then
			print("ok")
			break
		  end
		end
	  end)()  -- "AgentE382: You were missing an `end` statement."
	end
  end
end
Best way:
Spoiler
validSlotNumber = {}
for i = 1, 16 do
  validSlotNumber[ i ] = true
end

if numb == nil then
  while true do
	evt, pl = os.pullEventRaw("key")
	if evt == "terminate" then
	  do  -- "AgentE382: NOTICE: This creates a new chunk. Lua treats chunks as anonymous global vararg functions. Therefore, this is a cleaner syntax for achieving exactly the same thing."
		print("are you sure? Y/N")
		while true do
		  local evt, pl = os.pullEventRaw()
		  if pl == "y" or "Y" then
			return
		  elseif pl == "n" or "N" then
			print("ok")
			break
		  end
		end
	  end
	end
  end
end
Note: I'm using quotation marks to highlight my comments because of the broken syntax highlighting this forum uses.

Nope. return exits functions not do chunks. You can't exit do using break or return.

EDIT: It's not the Best way to do this.
Edited on 01 October 2013 - 04:36 AM