31 posts
Location
Brazil
Posted 17 May 2014 - 09:09 PM
Hi,
I'm clearly new to lua and I can't figure out how to make my program work like the "excavate" what I want is to make my program call with the variable that corresponde to how many times I want it to loop. Just like excavate 3 for example.
Can someone help me?
p.s.: Forgive my english faults, not my native language
259 posts
Posted 18 May 2014 - 02:17 AM
Look into tArgs
tArgs = {...}
if tArgs[1] == 1 then
print("uno")
else
print("spanish for not one")
end
then if you run <filename> 1 it will print "uno"
259 posts
Posted 18 May 2014 - 02:23 AM
Basically {…} makes a table based off parameters called. and you can access those parameters with <var>[number]. I'll show you below, but this is something you would use if you wanted to make a function with an unset amount of parameters.
-- I showed you in my last comment how to use it when running a program.
-- this will show you how to use it in a function
local function helper(...)
local t = {...} -- makes a table with the parameters put in when the function is called.
for i = 1, #t do -- loop through all of t. using # tells it to basically loop through the whole table.
print(t[i]) -- all the parameters
end
end
helper("hello", "world", "what", "a", "great", "day")
-- outputs
hello
world
what
a
great
day
31 posts
Location
Brazil
Posted 18 May 2014 - 02:26 AM
Thank you so much, that was exactly what i was looking for
31 posts
Location
Brazil
Posted 18 May 2014 - 02:28 AM
How do I count the number of args that was inputed?
1220 posts
Location
Earth orbit
Posted 18 May 2014 - 03:10 AM
You can use '#'
local numberOfArguments = #tArgs
the value in numberOfArguments will then be equal to the number of arguments passed
fun fact: 'tArgs' is commonly used as shorthand for 'table of arguments'
31 posts
Location
Brazil
Posted 20 May 2014 - 03:13 AM
The arguments can't be words? Or i'm just not making it right?
3057 posts
Location
United States of America
Posted 20 May 2014 - 04:12 AM
The arguments are strings. They are stored in a table.
print( ... ) --#passes ALL arguments to the print function
tArgs = { ... } --#make a table of the arguments so we can use them individually
print(tArgs[1]) --#passes the first argument to the print function
print(#tArgs) --#passes number of arguments to the print function
tArgs[1] = tonumber(tArgs[1]) --#convert the argument to a number if possible
Edited on 20 May 2014 - 02:13 AM
656 posts
Posted 20 May 2014 - 06:24 AM
The arguments can't be words? Or i'm just not making it right?
You get a table from {…} and in that table there can be any type of variable that you pass to the program.
testProgram 1 abcd true
Inside the program:
args = {...}
for i, j in pairs(args) do
print(type(j))
end
Output:
number
string
boolean
–EDID Fixed in pairs code
Edited on 22 May 2014 - 03:20 PM
259 posts
Posted 22 May 2014 - 04:36 PM
The arguments can't be words? Or i'm just not making it right?
You get a table from {…} and in that table there can be any type of variable that you pass to the program.
testProgram 1 abcd true
Inside the program:
args = {...}
for i, j in pairs do
print(type(j))
end
Output:
number
string
boolean
You forgot to pass in pairs a table
656 posts
Posted 22 May 2014 - 05:21 PM
The arguments can't be words? Or i'm just not making it right?
You get a table from {…} and in that table there can be any type of variable that you pass to the program.
testProgram 1 abcd true
Inside the program:
args = {...}
for i, j in pairs do
print(type(j))
end
Output:
number
string
boolean
You forgot to pass in pairs a table
Lol, noob-derp. Fixed.
259 posts
Posted 25 May 2014 - 01:33 AM
Lol, noob-derp. Fixed.
I figured you just quickly typed it up. I've done that several times.
7508 posts
Location
Australia
Posted 25 May 2014 - 01:35 AM
The arguments can't be words? Or i'm just not making it right?
You get a table from {…} and in that table there can be any type of variable that you pass to the program.
testProgram 1 abcd true
Inside the program:
args = {...}
for i, j in pairs(args) do
print(type(j))
end
Output:
number
string
boolean
–EDID Fixed in pairs code
well actually the output would be
string
string
string
you have to convert the arguments to the data type that you want. adding this code before your pairs loop would result in the output you stated
args[1] = tonumber(args[1])
args[3] = args[3] == "true"
Edited on 24 May 2014 - 11:36 PM