Comparing a Number to a String

Place to get help with not working mods / modding interface.
User avatar
DedlySpyder
Filter Inserter
Filter Inserter
Posts: 254
Joined: Fri Jun 20, 2014 11:42 am
Contact:

Comparing a Number to a String

Post by DedlySpyder »

So, I have a "string" that is always a number (a page number), and sometimes when I try to reference it, everything goes smoothly, other times I get an error for comparing a string to a number.

This works:

Code: Select all

local page = selectionFrame.pageNumber.caption
if (#global.avatars > page*10) then
	updateGUI(player, page+1)
end
But this returns an error for comparing a string to a number

Code: Select all

local page = selectionFrame.pageNumber.caption
if (page > 1) then
	updateGUI(player, page-1)
end
If I try to do some math before I compare in the second example (by adding 0 or multiply by 1), then it doesn't error. BUT, the number I get is not accurate. It goes from 2 to 91, 891, 8891, etc. A qucik check for LUA says that comparing a string to a number is fine (assuming that the string is really a number). I'm at a loss.
User avatar
steinio
Smart Inserter
Smart Inserter
Posts: 2638
Joined: Sat Mar 12, 2016 4:19 pm
Contact:

Re: Comparing a Number to a String

Post by steinio »

I suppose a string times 10 is 0 what is smaller #global.avatars everytime.

Acoording to http://stackoverflow.com/questions/1096 ... ing-to-int there is a function tonumber which you can use.

Greetings steinio
Image

Transport Belt Repair Man

View unread Posts
User avatar
DedlySpyder
Filter Inserter
Filter Inserter
Posts: 254
Joined: Fri Jun 20, 2014 11:42 am
Contact:

Re: Comparing a Number to a String

Post by DedlySpyder »

Ok, so the first bit of code always worked, and I found out that the second bit would work if I did useless math (page*1) ahead of time, I was just ruining the number later on.

Your function does the same as the useless math though, and looks much nicer, so thanks for the help.
Post Reply

Return to “Modding help”