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

Having some Lua Trouble

Started by Scratch, 25 October 2012 - 10:10 PM
Scratch #1
Posted 26 October 2012 - 12:10 AM
okay, So i'm trying to get a program to copy a file from a disk and paste it on the rom

Only difference is, I copy it 50 times.

i have 2 integers,


numLoop = 1
numStop = 50

I then make a string of the file copy destination


stringTempFile = "tempfile"..tostring(numLoop)..

the file is then copied to the rom

fs.copy("/disk/file", stringTempFile)

I get the response error of startup:41: Expected string, string
(sometimes when playing around with the code I get a "got string, nil" response)

Entirely the code is

numLoop = 1
numStop = 50
repeat
--stringLoop = tostring(numLoop
stringTempFile = "tempfile"..tostring(numloop)
fs.copy("/disk/file", stringTempFile)
print("Coping File "..numLoop" of "..numStop..)
numLoop = numLoop + 1
sleep(0.3)
until numLoop == numStop
jag #2
Posted 26 October 2012 - 12:28 AM
It would be much easier I think to use a for loop
But can you explain more detailed about what you want to happen?
remiX #3
Posted 26 October 2012 - 12:31 AM
Post the full code of the 'startup' program. seeming as the error is on line 41 of startup
Scratch #4
Posted 26 October 2012 - 12:55 AM
It would be much easier I think to use a for loop
But can you explain more detailed about what you want to happen?

Okay, so I have the one file 'file' on my disk
the program copies the file over to the Rom as tempfile1,
ever pass of the loop will copy over the file again as tempfile2, tempfile3, tempfile4 and so on

Post the full code of the 'startup' program. seeming as the error is on line 41 of startup

I'm running the game on a different computer Without a internet connection, which sucks
Line 41 is fs.copy("/disk/file", stringTempFile)
Kingdaro #5
Posted 26 October 2012 - 01:17 AM
For whatever reason, stringTempFile might not exist when you're using fs.copy on line 41.

I think it's because, on line 40, you wrote "tostring(numloop)" when it should be "tostring(numLoop)".

Honestly, though, you should just use a for loop and make the process much less complicated.


numStop = 50
for numLoop=1, numStop do
	stringTempFile = "tempfile"..tostring(numLoop)
	fs.copy("/disk/file", stringTempFile)
	print("Coping File "..numLoop.." of "..numStop)
	sleep(0.3)
end
ChunLing #6
Posted 26 October 2012 - 01:32 AM
What's with the extra concatenation operators on the ends of your strings?
Scratch #7
Posted 26 October 2012 - 06:12 AM
Okay, after a few hours of being disconnected and unable to check the forums

I managed to some how fix it. (Mind the awful format, I'm a messy programmer)


numLoop = 1
numStop = 50
sleep(0.5)
repeat --Loop Start
 --print(numLoop) --Debug
 stringLoop = tostring(numLoop)
 print("Copying File  "..numLoop.." of "..numStop)
 stringTempFile = "tempfile"..numLoop..
 print(stringTempFile) --Debug
 fs.copy("/disk/file", stringTempFile)
 --print("Copied")
 numLoop = numLoop + 1
 --print(numLoop)
 --write("Copying File"..numLoop.." of "..numStop)
 sleep(0.01)
 until numLoop == numStop + 1 --Loop End

It works, only there are two strange things happening":
I get a concatenation error appearing if " print(stringTempFile) –Debug " is commented out AND
all outputs of stringTempFile have a extra 1 printed on the end of it (but numLoop is fine)

Might try out Kingdaro's suggestion later
ChunLing #8
Posted 26 October 2012 - 07:41 AM
print() automatically calls a tostring() conversion in certain cases. In this case, stringTempFile contains a string concatenated with a number concatenated with…nothing. print() is able to turn that into a string, but you can remove it if you also remove the end concatenation, probably. That should also remove the mysterious "1" you're getting.