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

An unusual end error

Started by ShadowDisruptor, 17 September 2014 - 07:07 PM
ShadowDisruptor #1
Posted 17 September 2014 - 09:07 PM
Now, before you judge me for not being able to figure out an end error, I already checked my ends. I did by hand as well as with notepad++. From what I can see, all my ends are there. The exact error: 8: 'end' expected (to close 'function' at line 2)

Before you open spoiler: the code is 46 lines, couldn't shorten it due to me not knowing where the error is. Also, using the tekkit modpack, which really dosen't matter because the issue isn't with the mods codes.
Spoiler

--Start of setup
local function isReactor(input)
   local checkReactor = peripheral.wrap(input)
   checkReactor = checkReactor.getActive()
   return checkReactor
   checkReactor = nil  
end
local function whatIs(input)
   local reactor = peripheral.wrap(input)
   local whatIsIt = reactor.isActivelyCooled()
   if whatIsIt then
	  table.insert(activeReactors, input)
   else
	  table.insert(passiveReactors, input)
   end
   whatIsIt = nil
   reactor = nil
end

local bigReactors = {}
local passiveReactors = {}
local activeReactors = {}
local turbines = {}

--End of setup
--Sorting peripherals
foundPeripherals = peripheral.getNames()
for i=1,#foundPeripherals do
   local ok, err = pcall(isReactor(foundPeripherals[i]))
   if ok then
	  table.insert(bigReactors, foundPeripherals[i])
   else
print(foundPeripherals[i].." Is not a reactor!")
   end  
end
foundPeripherals = nil
ok = true
for i=1,#bigReactors do
   local ok, err = pcall(whatIs(bigReactors[i]))
   if not ok then
	  table.insert(turbines, bigReactors[i])
   end
end
print("Turbines found: "..#turbines)
print("Active reactors found: "..#activeReactors)
print("Passive reactors found: "..#passiveReactors)
Edited on 17 September 2014 - 07:08 PM
Dragon53535 #2
Posted 17 September 2014 - 09:12 PM
Found your error

   return checkReactor
   checkReactor = nil
Using return gets out of the function entirely, so setting checkReactor to being nil after returning messes with the lua interpreter

Also when you set checkReactor as a local variable, when you leave the function it's automatically set to nil, so you don't have to reset it back to nil.
Edited on 17 September 2014 - 07:14 PM
Dog #3
Posted 17 September 2014 - 09:20 PM
I've been over your code several times and I can't see what's causing the 'end' expected error, but I can see a couple of other things you may want to address:

1. You don't need to nil your variables if they are declared local within the scope of a function - when the function exits the variables will be discarded automatically
2. In your first function (isReactor) you use checkReactor both to wrap the peripheral and to store the result of your check on the wrapped device. Since you aren't going back and referencing the wrapped peripheral again it probably isn't technically a problem, but it's good practice not to re-use a variable like that. I'd recommend changing the wrapping variable name to something like tmpReactor or something else that makes sense.

FWIW, I hope that helps :)/>

EDIT: semi- :ph34r:/> 'd


Using return gets out of the function entirely, so setting checkReactor to being nil after returning messes with the lua interpreter
Are you sure about that? I'm no expert (by any stretch of the imagination), but my understanding is that when a return is invoked, everything after the return is ignored. However, looking at it, I can see how that might confuse the interpreter if my understanding is incorrect.

EDIT2: The more I look at it, the more Dragon's answer makes sense - it's the only thing I can see that would seem to be the cause. I learned something new today :D/>
Edited on 17 September 2014 - 07:27 PM
flaghacker #4
Posted 17 September 2014 - 09:33 PM
That is correct:

function ()
  if something then
    return
  end
  --other code
end

This is correct, but notice how the return is directly followed by the end that closes the if statement?


function ()
  if something then
    return
    --other code
  end
  --other code
end
This would trow the error, as the end is no longer directly followed by the end.

It expects an end after the return, an end expected
Edited on 17 September 2014 - 07:33 PM
Dog #5
Posted 17 September 2014 - 09:33 PM
Thanks for the clarification, flaghacker :)/>
ShadowDisruptor #6
Posted 17 September 2014 - 09:37 PM
2. In your first function (isReactor) you use checkReactor both to wrap the peripheral and to store the result of your check on the wrapped device. Since you aren't going back and referencing the wrapped peripheral again it probably isn't technically a problem, but it's good practice not to re-use a variable like that. I'd recommend changing the wrapping variable name to something like tmpReactor or something else that makes sense.
I did this on purpose to try and make the program more efficient.
Found your error

   return checkReactor
   checkReactor = nil
Using return gets out of the function entirely, so setting checkReactor to being nil after returning messes with the lua interpreter

Also when you set checkReactor as a local variable, when you leave the function it's automatically set to nil, so you don't have to reset it back to nil.
Thanks for the help!
Bomb Bloke #7
Posted 18 September 2014 - 01:41 AM
I did this on purpose to try and make the program more efficient.

Since "checkReactor" is local to "isReactor", it's automatically cleared whenever that function ends. There's no need to do it yourself.