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

Passing Variables to another program when calling it to run

Started by Smtgr14, 16 November 2012 - 06:30 AM
Smtgr14 #1
Posted 16 November 2012 - 07:30 AM
Forgive me if I titled this wrong, Lua is my first programming language.

What I would like to know is how to pass a variable to a program that I call to run with os.run()

Here is a bsic example of what I am trying to figure out:

programA
-- do stuff
variable1 = "1234"
os.run( , "programB") -- Run "programB"


programB
--do stuff
variable2 = varable1 --from the first program
print(variable2)


I apologize if I am being too vague. If you require clarification, please ask.
I would really apreciate it if someone could explain how this works, I tried looking at the documentation but that didn't help.

Thanks in advance,
Smtgr14
Cranium #2
Posted 16 November 2012 - 07:55 AM
I fI got you right, you want to run a program with arguments.
Simple.
Just add local tArgs = {…} at the top. This will put anything you enter AFTER the name of the program into this table.
So if you use os.run("programB", "variable1") it will put variable1 into the first position of the table. so variavle1 would be tArgs[1].
Lyqyd #3
Posted 16 November 2012 - 09:16 AM
As a point of clarification, you would actually call os.run({}, "programName", variableName), so that the variable's contents would be used rather than the name.
Cranium #4
Posted 16 November 2012 - 09:19 AM
Indeed. I don't really use os.run myself, so I'm not positive on my usage.
Kingdaro #5
Posted 16 November 2012 - 10:27 AM
I always believed that using shell.run() was better practice if you don't actually have any environment variables. :/
Lyqyd #6
Posted 16 November 2012 - 11:13 AM
I would agree, Kingdaro. I was being consistent with the code in the original post, but in this case, the use of shell.run() instead would probably be a better choice.
Sammich Lord #7
Posted 16 November 2012 - 12:49 PM
I fI got you right, you want to run a program with arguments.
Simple.
Just add local tArgs = {…} at the top. This will put anything you enter AFTER the name of the program into this table.
So if you use os.run("programB", "variable1") it will put variable1 into the first position of the table. so variavle1 would be tArgs[1].
I know how to use the args table in the top of the script, but does the shell or BIOs pass it on as a var to the script? Also, does it have to be labeled "args" for it to work, or is it just the first table in the script?
faubiguy #8
Posted 16 November 2012 - 01:27 PM
Lua passes it to the script, its the construct for having functions with a variable number of arguments. And it has to be a table with three periods in it.
Sammich Lord #9
Posted 16 November 2012 - 01:33 PM
Lua passes it to the script, its the construct for having functions with a variable number of arguments. And it has to be a table with three periods in it.
So the file is opened with the dofile function, and is passed with the args that the shell program gets? For example if you type "edit <filename>" the shell splits the entries and then passes it to the file?
Cranium #10
Posted 16 November 2012 - 01:57 PM
I know how to use the args table in the top of the script, but does the shell or BIOs pass it on as a var to the script? Also, does it have to be labeled "args" for it to work, or is it just the first table in the script?
Nah. Anything in … is the additional arguments you are running the script with. You can use it just in a function, or as a variable. But it is better to use it as a table, so you can use it more effectively. It does not have to be named args or anything like that, it just helps when writing the code. You could name it fluffyKitty = {…} and it would still return the same values.
Shaftman #11
Posted 16 November 2012 - 02:52 PM
Forgive my ignorance, but this question is quite similar to a question I have.

If I have defined a string 'message'

id, message, distance = rednet.receive()
and I want to treat it as a command and run it locally, would that be something like:

shell.run(command)
or do I need to do something additional to prepare the string for execution? I am assuming that the string is something like "gps host" in that it should have one valid program possibly followed by valid arguments for said program.
Actually, that's a rhetorical question; I know that I cannot run this as I have tried already – what is the proper way to do this?
I'm not new to programing in general, but my lua is very rusty and I've just started trying to implement some basic remote control functionality.
Sammich Lord #12
Posted 16 November 2012 - 09:44 PM
Forgive my ignorance, but this question is quite similar to a question I have.

If I have defined a string 'message'

id, message, distance = rednet.receive()
and I want to treat it as a command and run it locally, would that be something like:

shell.run(command)
or do I need to do something additional to prepare the string for execution? I am assuming that the string is something like "gps host" in that it should have one valid program possibly followed by valid arguments for said program.
Actually, that's a rhetorical question; I know that I cannot run this as I have tried already – what is the proper way to do this?
I'm not new to programing in general, but my lua is very rusty and I've just started trying to implement some basic remote control functionality.
If you mean run the code in the msg then you would have to do something like this:


id. msg = rednet.receive()
hello = loadstring(msg)
hello()
That will run the code that is sent in the message as a function.
Sammich Lord #13
Posted 16 November 2012 - 09:50 PM
I know how to use the args table in the top of the script, but does the shell or BIOs pass it on as a var to the script? Also, does it have to be labeled "args" for it to work, or is it just the first table in the script?
Nah. Anything in … is the additional arguments you are running the script with. You can use it just in a function, or as a variable. But it is better to use it as a table, so you can use it more effectively. It does not have to be named args or anything like that, it just helps when writing the code. You could name it fluffyKitty = {…} and it would still return the same values.
I thought any table or function with "…" in it meant that you can have any number of args. For example I could have a function like this

function hello(...)
  print(...)
end
It is hard to find explanations on features in Lua, so I tend to have to ask people on the forums or try to google it for about 30 minutes.
Sammich Lord #14
Posted 16 November 2012 - 09:58 PM
Accidental double post.
Espen #15
Posted 17 November 2012 - 12:16 AM
It is hard to find explanations on features in Lua, so I tend to have to ask people on the forums or try to google it for about 30 minutes.
I find a great source of information to be these two links:
Lua PIL - The go-to reference manual.
Roblox Lua - Nice to your eyes and with good examples.

For example, regarding variable arguments passed to a function: Lua PIL - 5.2 – Variable Number of Arguments
MaHuJa #16
Posted 17 November 2012 - 01:10 AM
I thought any table or function with "…" in it meant that you can have any number of args. For example I could have a function like this

function hello(...)
  print(...)
end

That's true for functions. And a file is basically a big function(…)

{ … } makes a table as usual, but just fills in with the number of arguments passed to it. It is no different from {somefunc()} in that somefunc can return several values (as explained in http://www.lua.org/pil/5.1.html )