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

Help With A Copy Program

Started by Zenon, 29 June 2015 - 06:48 PM
Zenon #1
Posted 29 June 2015 - 08:48 PM
Hi All! Im Trying To Make A Copy Function With An Infinite Table.

Code:

x = 1
function input()
inp = {}
loop = true
while loop == true do
  inp[x] = read()
  tostring(inp[x])
  x = x + 1
  loop = false
end
end
term.write("Copy Which File?: ")
--input1 = read()
input()
term.write("To Where: ")
--input2 = read()
input()
fs.copy(inp[1], inp[2])
If I Do This Then I Get The Error, "Expected string, string"

Also, If I Unpack it Like This:

x = 1
function input()
inp = {}
loop = true
while loop == true do
  inp[x] = read()
  tostring(inp[x])
  x = x + 1
  loop = false
end
end
term.write("Copy Which File?: ")
--input1 = read()
input()
term.write("To Where: ")
--input2 = read()
input()
fs.copy(unpack(inp[1]), unpack(inp[2]))
I Get The Error "bad arguement: table expected, got nil"

Anyone Know How To Fix This?
TheOddByte #2
Posted 29 June 2015 - 09:05 PM
I don't know why you're even using a table and a loop in the first piece of code, but it's pretty obvious that it exits out of the loop before inp[2] gets declared
I'd suggest you do this, so that it checks that the lenght of the table actually is 2, then break out of the loop
And you don't even need the x variable in this case, as you can just use tabel.insert.

while true do
    local input = read() --# Get the input
    table.insert( inp, input ) --# Add it into the table
    if #inp == 2 then --# Check if the table's lenght is 2
        break --# Break out of the loop if it was
    end
end


About the second piece of code, you need to read more about unpack, because what it does is that it unpacks tables, not strings/numbers etc. :P/>

fs.copy( unpack( inp[1] ), unpack( inp[2] ) )
Find anything wrong with that line? You're accessing the index 1 and 2 in the table inp, those are strings, and thus it throws an error.
You could solve this by changing that line to

fs.copy( unpack( inp ) )


One last thing, I see that you did this

tostring( inp[x] )
This won't do anything, if inp[x] was a number, it would still be a number because you haven't assigned it to the tostring call
So you need to do this:

inp[x] = tostring( inp[x] )
Zenon #3
Posted 29 June 2015 - 09:48 PM
Im More Or Less Doing This As A Test, Not A Practical Approach :P/>

Also,

inp[x] = tostring( inp[x] )
This Doesnt Seem To Work, Still Drawing The Error:
"Expected string, string"


while true do
    local input = read() --# Get the input
    table.insert( inp, input ) --# Add it into the table
    if #inp == 2 then --# Check if the table's lenght is 2
	    break --# Break out of the loop if it was
    end
end
Ill Try This Out When Im On My Computer Later, Thanks!
TheOddByte #4
Posted 29 June 2015 - 09:56 PM

inp[x] = tostring( inp[x] )
That part would not fix the whole code, it would only turn the variable into a string( if it would be a number ).
The error is here

fs.copy( inp[1], inp[2] )
where one of the variables is not a string, my guess would be that it's inp[2]

Try to debug with something like this

if not inp[1] then
	error( "missing the first parameter", 0 )
elseif not inp[2] then
	error( "missing the second parameter", 0 )
end
fs.copy( inp[1], inp[2] )

And I should've mentioned this earlier, but you should specify which line you're getting the error at, as it makes it easier for people to find the error and help you.
Edited on 29 June 2015 - 07:57 PM