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

Shell.run With Variable Arguments

Started by maninorange, 10 November 2013 - 03:56 AM
maninorange #1
Posted 10 November 2013 - 04:56 AM
Most of my programs are small, and so I typically find myself scripting several previously used programs together for higher purposes… and the issue I'm having is that I need to run a subroutine with a variable number of arguments. shell.run will only accept strings as arguments, so I can't give it the list from the fs API directly (I tried), toString() doesn't seem to exist, and in an atmosphere of hopelessness, I tried stringing (heh) the table together into one gigantic string. This also failed.

Program 1: pt

local Args = { ... }
fs.makeDir("/Jfunctions")
for n=1,#Args do
  if not fs.exists("/Jfunctions/" .. Args[n]) then
	fs.copy("disk/" .. Args[n] , "/Jfunctions/" .. Args[n])
  else
	Args[n] = "EXISTS: " .. Args[n]
  end
end
print("Functions ported:")
for n=1,#Args do
print("  " .. Args[n])
end


Program 2: ptAll

local passTable = fs.list("/disk")
local passString = ""
for n=1, #passTable do
  passString = passString .. passTable[n]
end
shell.run("pt" , passString)


The only difference for the first version of ptAll was that none of that string junk was in there, and i just tried to pass the table directly.
As far as the problem that this creates goes, there's a simple solution in [A] running pt multiple times and editing it so it would look less annoying to do so, or , rewriting a similar function into the body of ptAll. This is less about the current issue than it is about the countless future issues resulting from not knowing how to pass variable arguments to a subroutine.
Thanks.
maninorange #2
Posted 10 November 2013 - 05:12 AM
I actually lucked out on this one, and found an answer in a somewhat related question, here:
http://www.computerc...es-to-programs/
This is the code I should have been using for ptAll:
local passTable = fs.list("/disk")
shell.run("pt" , unpack(passTable))
It works beautifully.
Edited on 10 November 2013 - 04:13 AM
Bomb Bloke #3
Posted 10 November 2013 - 05:58 AM
You've found your answer, but a few points that might be of interest to you:

Let's say your folder had two files in it, "file1" and "file2". The first "ptAll" script you listed would've simply shoved them together to create a string that read as "file1file2" - this would be rather difficult to convert back into individual values under any circumstance. I get the impression you were expecting the concatenation to give some sort of different result.

There is a "tostring()" (note the lowercase "s"), but to the best of my knowledge it's no good for tables (I've only had a use for it in converting numeric variables). The function you were originally after is textutils.serialize(), which you'll still likely find useful if you ever want to write a table to disk.

I'm assuming you're already familiar with creating functions and believe you have reason to use shell.run() instead, but it seems to me that building your own API(s) would be a better choice. Here's a guide on the topic.
Edited on 10 November 2013 - 04:59 AM
maninorange #4
Posted 10 November 2013 - 07:30 AM
Yeah, the attempt with making it all into one string was really just out of desperation for lack of having a better idea.
The REAL problem here is that I'm doing a horrible job at finding the documentation for this. Serialize will come in handy starting now.
Thank you very much!