11 posts
Posted 18 December 2012 - 05:20 AM
I started tinkering around with accepting user inputs today. Getting a user's input seems to be easy, however getting multiple arguments at program start up seems to be a bit trickier. I know the tunnel program that each mining turtle comes with can already accept an argument by storing the arg in a table. So I'm wondering how would I go about storing multiple args at the same time?
Tunnel code:
local tArgs = { ... }
if #tArgs ~= 1 then
print( "Usage: tunnel <length>" )
return
end
302 posts
Posted 18 December 2012 - 05:31 AM
… is a vararg, a weird symbol that holds a variable number of arguments; when running a program from the shell, e.g. myprogram 1starg 2bdarg ect, the program file is loaded into a function, say f, and that function is called like this: f(1starg,2ndarg, ect). In you program, the symbol … allows you to access those arguments by:
-re-using them: other_f(…); selecting/getting their number with the select function (see the Lua reference manual); storing them in a table: targs={…}, so that targs[1] is 1stargs, ect.
11 posts
Posted 18 December 2012 - 05:50 AM
Mind giving me an example of storing things in this table? I just began working with lua yesterday. Still getting the formatting and syntax down.
302 posts
Posted 18 December 2012 - 06:27 AM
Well, when you call the program from the shell, you write
program_name arg1 arg2 arg3 (ect; for the sake of simplicity I'll assume there's only 2 arguments)
The program code for using the arguments must not be inside a function of the program. You can do it like:
-:
arg1,arg2=…
(and you use arg1, arg2)
Or
arg1=…
If you just want the first argument.
Or
args={…}
arg1=args[1]
arg2=args[2]
Or
arg2=select(2,…)
If you don't even know how to deal with tables, I suggest you start with a small tutorial.
2088 posts
Location
South Africa
Posted 18 December 2012 - 06:32 AM
You can have as many arguments as you want.
Args = {...} -- the ... is for the arguments and {} stores them into a table
Args[1] will be the first argument
Args[2] will be the second argument
so on...
11 posts
Posted 18 December 2012 - 07:08 AM
Yep figured it out, thanks for the tips.
local tArgs = { ..., ... }
-- #ArrayName returns the size of the array
x = tonumber(tArgs[1])
y = tonumber(tArgs[3])
For some reason though, I need to use an index of 3 to access the second argument. Any reason behind this?
8543 posts
Posted 18 December 2012 - 07:11 AM
What command are you using to start the program?
302 posts
Posted 18 December 2012 - 07:21 AM
Yep figured it out, thanks for the tips.
local tArgs = { ..., ... } [i]--incorrect. use {...} only. The way it is now, tArgs={arg1,arg1,arg2}[/i]
-- #ArrayName returns the size of the array
x = tonumber(tArgs[1])
y = tonumber(tArgs[3])
For some reason though, I need to use an index of 3 to access the second argument. Any reason behind this?
11 posts
Posted 18 December 2012 - 07:29 AM
Right now I'm making it take two arguments, that fill in the variables x and y.
input 5 3
It assigns 5 as x, and 3 as y. I'm guessing it assigns the space as the second index in the table, which makes me wonder why it would ever consider that as an argument.
Edit: Ah, I see what you meant there Cool, thanks.