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

[LUA] [Solved] Nil Arg

Started by baeshra, 18 February 2013 - 06:57 PM
baeshra #1
Posted 18 February 2013 - 07:57 PM
Title: [LUA] Nil Arg
How would I go about setting a variable to 1 if no args are input?


arg = (...)
arg = tonumber(arg)
x = 0
while x < arg do
if turtle.forward() == true then
x = x + 1
end
end

I want the program to set arg to 1 if no arguments are originally input following the execution.
remiX #2
Posted 19 February 2013 - 04:38 AM
Well first to get arguments you use {…} not (…)
secondly, you need to index which one to use


arg = {...}
length = tonumber(arg[1]) or 1 -- will make it 1 if nil
baeshra #3
Posted 19 February 2013 - 04:41 AM
Thanks! Previously I've been using (…) for args and it works fine yet in this case it only works with {…}, why is that?
I think I understand now. {…} is used to store it as a series whereas (…) stores it as a single number, correct?

This is my new code and it seems to work. Should I just get in the habit of using {} for later when I need to deal with series'?

arg = (...)
arg = tonumber(arg) or 1
x = 0
while x < arg do
if turtle.forward() == true then
x = x + 1
end
end
remiX #4
Posted 19 February 2013 - 05:34 AM
{…} stores it into a table,
… is the actual arguments and {} is used to put it into a table