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

[lua][question] Create a program with a program

Started by Finnvos123, 23 April 2013 - 05:16 AM
Finnvos123 #1
Posted 23 April 2013 - 07:16 AM
I've tried different things but nothing worked. How do I make a program create another program?
Like:

fs.open("test", w)
code to write the program
fs.close
Lyqyd #2
Posted 23 April 2013 - 07:18 AM

local handle = io.open("outfile", "w")
if handle then
  handle:write([[
print("Hello, World!")
]])
  handle:close()
end
Mads #3
Posted 23 April 2013 - 07:19 AM
EDIT: Damn ninjas.

You can use this simple piece of code:


local f  = io.open("test", "w")

f:write([[
	-- program code here
	print("Hello from test!")
]])

f:close()
Finnvos123 #4
Posted 23 April 2013 - 07:21 AM
Could you also put comments in the code so it is easier to understand?

EDIT: nvm, I figured it out. Thanks for the quick help
remiX #5
Posted 23 April 2013 - 07:23 AM
Yeah

local handle = io.open("outfile", "w")
if handle then
    handle:write(
[[
-- Declare a function
local function foo( s )
    print( s )
end

foo( "Hello!" )
]])
    handle:close()
end
Finnvos123 #6
Posted 23 April 2013 - 07:31 AM

local f = fs.open("startup", w)
f:write([[
	print"Starting...."
	sleep(1)
]])
f:close()
With this code I get:
test:1: Expected string, string
Mads #7
Posted 23 April 2013 - 07:32 AM
Put quotes around w.
Finnvos123 #8
Posted 23 April 2013 - 07:37 AM
Somehow the program/file it creates is completely empty.
After some testing I found out that it doesn't do anything to the file.
remiX #9
Posted 23 April 2013 - 07:49 AM
Hmmm…if your full code is the above with just the "w" replacement. It really should work…

Have you tried restarting the PC? Lol typical IT solution. Might you have edited io somehow.

Try fs?

local f = fs.open("startup","w")
w.write([[print"Starting...."
sleep(1)]]
)
w.close()

Really the same thing…
Have you changed any ComputerCraft files?
Finnvos123 #10
Posted 23 April 2013 - 07:51 AM
And by restarting it suddenly worked.
HOW!?
remiX #11
Posted 23 April 2013 - 08:06 AM
Haha. You probably overwrote something sometime…