Jump to content




Attempt to compare number with string expected, got number


  • You cannot reply to this topic
7 replies to this topic

#1 Creeper9207

  • Members
  • 211 posts

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()
Attached Image: 2015-02-26_06.11.00.png
Attached Image: 2015-02-26_06.10.43.png

Edited by Creeper9207, 25 February 2015 - 11:51 AM.


#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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.

#3 Creeper9207

  • Members
  • 211 posts

Posted 25 February 2015 - 11:49 AM

Too bad, it does

Lua Loves to hate me </3

#4 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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.

#5 Creeper9207

  • Members
  • 211 posts

Posted 25 February 2015 - 11:52 AM

let me upload screenshots...

here:

the screenshots are there

Full proof

#6 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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])


#7 Creeper9207

  • Members
  • 211 posts

Posted 25 February 2015 - 12:00 PM

hm, i tried tonumbering i, welll, watevs, thanks!

#8 Dragon53535

  • Members
  • 973 posts
  • LocationIn the Matrix

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





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users