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

[Lua][Question] How Can I Get the Return Statement's Value From Another Program?

Started by Vorsaykal, 17 June 2012 - 03:52 AM
Vorsaykal #1
Posted 17 June 2012 - 05:52 AM
If I have a program called "return", with code like:

return 100
How can I get 100 from this program in a different program?

I tried:

shell.run("return")

print(shell.run("return"))

a, b, c = shell.run("return"))
print(a, b, c)

And I couldn't get 100 from any of them. I have absolutely no clue how to do it, as it doesn't work like any other programming language I've ever dealt with.
kazagistar #2
Posted 18 June 2012 - 03:43 PM
The command you are looking for is loadfile(). Check it out in the lua documentation here and here. Something like this should work:

a = assert(loadfile("return"))()
Vorsaykal #3
Posted 19 June 2012 - 01:50 AM
Hmm… try this code:

Return Program:

return_code = 100

Actual Program:

shell.run("return")
if return_code == nil then
print("This test has failed.")
else
print("Success!")
print(return_code)
end

Perfect. Thanks!
diosmio #4
Posted 02 February 2013 - 08:09 AM
Is there no other way to get return values from a program called form another program?
Cause i dont want to use global variables.

I want to call a program with args and get a return value back.

Someone got an idea?
Kingdaro #5
Posted 02 February 2013 - 08:12 AM
Is there no other way to get return values from a program called form another program?
Cause i dont want to use global variables.

I want to call a program with args and get a return value back.

Someone got an idea?
As stated by kazagistar, you can use loadfile().

There's also dofile(). The difference between the two is that loadfile() returns a function with the program's code, while dofile() immediately runs it.
diosmio #6
Posted 03 February 2013 - 03:29 AM
Yes. It works with:

returnvalue = assert(loadfile("programname"))("argument")

thx very much