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

Help

Started by macss_, 17 May 2014 - 07:09 PM
macss_ #1
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
CCJJSax #2
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"
CCJJSax #3
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

macss_ #4
Posted 18 May 2014 - 02:26 AM
Thank you so much, that was exactly what i was looking for
macss_ #5
Posted 18 May 2014 - 02:28 AM
How do I count the number of args that was inputed?
Dog #6
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'
macss_ #7
Posted 20 May 2014 - 03:13 AM
The arguments can't be words? Or i'm just not making it right?
KingofGamesYami #8
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
flaghacker #9
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
CCJJSax #10
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
flaghacker #11
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.
CCJJSax #12
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.
theoriginalbit #13
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