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

Inserting a parameter into a string

Started by bigbaddevil6, 23 April 2014 - 04:53 PM
bigbaddevil6 #1
Posted 23 April 2014 - 06:53 PM
The object is I'm making a universal function that do file writing, copy, and moving. I want the function to be able to take the program you want to transfer as a parameter and input that into the string that it write to the file.
For example this is my current code.
function transfer(type)
  fs.copy(type, "disk/"..type.."Prog")
  file = fs.open("disk/startup", "w")
  file.writeLine("fs.move(\"disk/\""..type.."\"Prog\","..type..")")
  file.writeLine("shell.run(\"receive \""..type..")")
  file.close()
end

The Object is that when I were to pass in a program name(lets say "build"). I want it where when it writes to the disk by the end the disk is reading.

fs.move("disk/buildProg", "build")

But when its done it reads.

fs.move("disk/"build"Prog", build)

I understand why it does that looking back and forth. Would I do a string.gsub to replace the text in the string with the one I want to be in there.

Something like
string.gsub("fs.move(\"disk/type/Prog\", \"type\")", "type", type)

I would do some variable changes to make it easier to read.
Edited on 23 April 2014 - 04:53 PM
Lyqyd #2
Posted 23 April 2014 - 07:43 PM
You're adding the problematic quotes in yourself. Why not just remove them from the statement that writes the string?
bigbaddevil6 #3
Posted 23 April 2014 - 08:11 PM
Yea. Taking another look at it I see. This is how I changed it.
file.writeLine("fs.move(\"disk/"..type.."Prog\", \""..type.."\")")
I believed I type that right when putting it here.
It gets difficult following along when doing something like that.