Jump to content




Return a string from a program


11 replies to this topic

#1 gigimoi

  • New Members
  • 1 posts

Posted 02 August 2012 - 06:44 PM

Is it possible to return a string from a program?
local string s = "Blah"
return s

print(shell.run("returnblah"))


#2 Rsstn

  • Members
  • 53 posts
  • LocationLondon, UK

Posted 02 August 2012 - 06:50 PM

No, that code would just print 'true'.
Never really thought about that before, but it's a shame that you can't...

#3 ElvishJerricco

  • Members
  • 803 posts

Posted 05 August 2012 - 07:38 PM

only thing you can really do is use global variables =/

#4 Noodle

  • Members
  • 989 posts
  • LocationSometime.

Posted 06 August 2012 - 10:01 AM

Why do you need a return "string"
??

#5 Rsstn

  • Members
  • 53 posts
  • LocationLondon, UK

Posted 06 August 2012 - 04:04 PM

You could just run the code as a function, I'm not sure why it's needed as a program.

#6 Noodle

  • Members
  • 989 posts
  • LocationSometime.

Posted 07 August 2012 - 04:10 PM

Example:
function pro()
  if powerLevel > 9000 then
	return true -- True would be the metaphorical "string" in this example. Cannot do any other way (except for nil + false)
  end
end
if pro then
  print("Pro")
end


#7 KFAFSP

  • Members
  • 42 posts
  • LocationGermany

Posted 07 August 2012 - 05:23 PM

Or you could do the following :

local ProgramPath = <PathHere>
local tEnv = {}
local Success = pcall(os.run, tEnv, ProgramPath)
if Success then
  -- Get the result
  local Result = tEnv.result
end

That requires that the file is a valid lua file, and that it doesnt use

return "stuff"

but :

result = "stuff"

note that you wont be able to execute it normally error-free. You would probably get an "attemptet to write to global" error.

#8 Pharap

  • Members
  • 816 posts
  • LocationEngland

Posted 08 August 2012 - 04:25 AM

There is one way to get around it.

Make the program you are trying to get the string from to write the string to a file, then have the other program read the string from the file.

So program one:(pseudocode)
Run("program2")
opedfile io.open(fileforreading, readmode)
stringvar = opedfile.readline()
io.close(file)

something close to that at least

#9 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 08 August 2012 - 07:06 AM

load the file as a function using loadfile(path) and then call the function, it should use the return command as normal

#10 KFAFSP

  • Members
  • 42 posts
  • LocationGermany

Posted 08 August 2012 - 07:16 AM

I think the Environment Access is much faster and also looks coded better. Although writing to a file reminds me of WIndows' STDIN and STDOUT Pipes. A shame that you can't do pipes in lua. There is prob. a way, but that means searching...

#11 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 08 August 2012 - 07:28 AM

What I mean is that instead of using shell.run(programname) use loadfile(programname)()

so if you name it "myprogram" and the file has:
if rs.getInput("back")==true then return "success" end

then simply
print(loadfile("path/myprogram")())

and if you have redsone input at the back it should print "success"

#12 KFAFSP

  • Members
  • 42 posts
  • LocationGermany

Posted 08 August 2012 - 10:12 AM

Thats the same as dofile! And thats the same as... os.run (almost) ?





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users