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

Attempt to compare number with string expected, got number

Started by Creeper9207, 25 February 2015 - 10:39 AM
Creeper9207 #1
Posted 25 February 2015 - 11:39 AM

args = { ... }
i = 1
if args[1] then
currentDir = args[1]
else
currentDir = "/"
end
if args[2] then
i = args[2]
end

function getFS()
listing = fs.list(currentDir)
for k, v in pairs(listing) do
if k < i+14 then
if i < k then
print (k .. " " .. v)
end
else
end
end
end
getFS()

line 16 attempt to compare number with string expected, got number, before you ask i have tried tonumber()
[attachment=2135:2015-02-26_06.11.00.png]
[attachment=2134:2015-02-26_06.10.43.png]
Edited on 25 February 2015 - 10:51 AM
Bomb Bloke #2
Posted 25 February 2015 - 11:45 AM
This code can't throw that error at that line. Ensure that it matches the code you have saved to the specific file you're attempting to run.
Creeper9207 #3
Posted 25 February 2015 - 11:49 AM
Too bad, it does

Lua Loves to hate me </3
Bomb Bloke #4
Posted 25 February 2015 - 11:49 AM
It will not - if the comparison on that line failed, then the line above would've also failed, and so line 16 would never have been reached.

You have either changed your code to match this then forgotten to save it before running it, or you're running a different script file entirely.
Creeper9207 #5
Posted 25 February 2015 - 11:52 AM
let me upload screenshots…

here:

the screenshots are there

Full proof
Bomb Bloke #6
Posted 25 February 2015 - 11:58 AM
Ah - I see. Odd that it doesn't say "string with number", because that's what you're trying to do.

Switch line 9 with:

i = tonumber(args[2])
Creeper9207 #7
Posted 25 February 2015 - 12:00 PM
hm, i tried tonumbering i, welll, watevs, thanks!
Dragon53535 #8
Posted 26 February 2015 - 09:35 AM
A couple things for your code. Just general improvements, since it seems like your problem is fixed.

1: You don't need an else for an if, which you know because of the if args[2] then. However for some reason you have an else inside your for loop that does nothing.

2: Use locals, it's good practice and they're loaded faster than global variables which you have now. It's exceptionally easy, just have to add the word local when you first declare the variable. There are tricky rules that go with locals though. I would check out this tutorial.

3: Something that most people do, and enjoy reading code that's like this, is indenting. When you hit a new code block (a function, loop, if statement) you increase the indentation level, and when hitting the end of those, the indentation level is reduced. It helps and allows you to see if somethings encased in something else. Example:

local function foo()
print("Hello")
end
for 1,2 do
print("World") --#This isn't going to be printed when I run foo. I wanted that.
end

local function bar()
  print("Hello there")
  for 1,2 do
	print("Everybody.")
  end
end
Do you notice how you can see that the for loop is inside the function on the second one more easily than the first? This helps you figure out the structure to a point without having to go through line by line. And you can make sure that you have the proper number of ends.

That's basically it, however there's still always more to learn :P/>