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

Running a program with arguments

Started by Swift7, 09 March 2013 - 11:32 AM
Swift7 #1
Posted 09 March 2013 - 12:32 PM
Title: Running a program with arguments

I have what would most likely be an incredibly easy question, but i'm new to lua, and I can't find anyone who has explained it. I basically have 2 programs, the main program("startup"), and the second program("crush") which I use for more redstone work, and together they make up a mob trap system.


What I want to do is pass a variable from the startup program, to the crush program. I know I can do:


local integer = 1
shell.run("crush", integer)


but I do not know how to receive it. I believe I have to create a function in the crush program:


function mobCrush(…)
{
print(integer)
}

In this case, I want the program to send the variable(integer) and then print it in the crush program (function/method if I have to use one). I am not sure if I have to use a function/method, but if someone would please be able to explain it to me it would be much appreciated.



P.S. I apologize if this was unclear, I am sort of new to programming in java, and I have not yet grasped the lua terms, so I hope someone can understand what I am trying to do.
Lyqyd #2
Posted 09 March 2013 - 02:03 PM
Split into new topic.

You need to catch the parameters using the vararg expression, `…`. You can catch the arguments in a table:


local args = {...}

Or in a single variable, or list of variables:


local arg = ...

This should not be in a function in your code (or it will be attempting to grab extra arguments given to that function instead of your program).
3LiD #3
Posted 09 March 2013 - 02:13 PM
Something like this would work:


local no = 50
shell.run("crush", tonumber(no))

Change the 50 in the variable no to the ingteger.
Swift7 #4
Posted 09 March 2013 - 03:05 PM
I suppose I should have included some code, because after subbing in both of these suggestions, I wasn't able to get either to work. Although that is most likely my fault.

This is the last bit of my code in the startup program:

local rsoutput = 1
local enter = ""


//Declaring variables at the very start of my code

print("Press enter to crush.")

if(enter == "") then
shell.run("crush", rsoutput)
end // End if statement

end //End while loop that this code is in.
I have tried putting quotation marks(" ") around rsoutput. I also tried using the shell.run("crush", tonumber(rsoutput)) as suggested above.

My problem is at the start of the code in my crush program:

local args = {...}			   //Not sure if args can have different names, so I just left it as was suggested
local rsoutput = args	   // I tried putting this in here just as a test, to see if I had to convert args for some reason, but I have a feeling it is a useless line of code

rs.setBundledOutput("back", args)		
// This is where the problem in my code is, note that in my actual code the value that is being sent to this program is not 1 as said above, but a combination of colours.

The rest of my code is fine, and I thought it would be easier to see the most likely obvious problem if I just included the section of my code that was malfunctioning.

Thank you very much for the help
Lyqyd #5
Posted 09 March 2013 - 05:09 PM
If you're going to catch the arguments in a table (which is unnecessary if you are only interested in the first argument), you would need to index the table: args[1]

I would suggest for this use that you use `local args = …` (removing the curly braces), as your code in the crush program should then work.
Swift7 #6
Posted 10 March 2013 - 08:49 AM
After playing around with it I got it working, thank you very much everyone!

I made some assumptions that I thought would work in lua because they work in java, but that wasn't the case.
Dlcruz129 #7
Posted 10 March 2013 - 10:01 AM
Also, comments in Lua are two dashes (–) instead of slashes.