what on earth..? equivalence (==) not working

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

what on earth..? equivalence (==) not working

frey
so i've got this function here:

function HandTrackOSCHandler:GetArmIndex( id )
        local idx = -1
        local k,v
        for k,v in pairs(self.arms) do
                local arm_id = v:GetId()
                print("'"..arm_id.."'","'"..id.."'","'"..k.."'")
                if id==arm_id then
                        print("found")
                        idx = k
                        break
                end
        end
        print("returning "..idx)
        return idx
end

i'm passing in a number i get from a substring pattern match.

when i run the code, having set up everything else all ok, the print
statements give me:

'235'   '235'   '1'
returning -1

in other words, even though 235 quite clearly == 235, the code inside
the if statement never executes!

why does 235 not equal 235? why isn't 235==235 true?

--
f r e y
live music with computers
http://www.frey.co.nz

Reply | Threaded
Open this post in threaded view
|

Re: what on earth..? equivalence (==) not working

David Morris-Oliveros-2
What about the types?
Does 235 == '235' ?

// David Morris-Oliveros
// Camera & Lua Coder
// [hidden email]



Damian Stewart wrote:

> so i've got this function here:
>
> function HandTrackOSCHandler:GetArmIndex( id )
>     local idx = -1
>     local k,v
>     for k,v in pairs(self.arms) do
>         local arm_id = v:GetId()
>         print("'"..arm_id.."'","'"..id.."'","'"..k.."'")
>         if id==arm_id then
>             print("found")
>             idx = k
>             break
>         end
>     end
>     print("returning "..idx)
>     return idx
> end
>
> i'm passing in a number i get from a substring pattern match.
>
> when i run the code, having set up everything else all ok, the print
> statements give me:
>
> '235'   '235'   '1'
> returning -1
>
> in other words, even though 235 quite clearly == 235, the code inside
> the if statement never executes!
>
> why does 235 not equal 235? why isn't 235==235 true?
>
Reply | Threaded
Open this post in threaded view
|

Re: what on earth..? equivalence (==) not working

Diego Nehab-3
In reply to this post by frey
Hi,

> why does 235 not equal 235? why isn't 235==235 true?

Are you sure you shouldn't be asking instead why "235"==235 is not true?

Regards,
Diego.
Reply | Threaded
Open this post in threaded view
|

Re: what on earth..? equivalence (==) not working

frey
In reply to this post by David Morris-Oliveros-2
David Morris-Oliveros wrote:
> What about the types?
> Does 235 == '235' ?

aah. it seems id is a string but arm_id is a number.

i thought lua converted automagically between the two types..?

--
f r e y
live music with computers
http://www.frey.co.nz

Reply | Threaded
Open this post in threaded view
|

Re: what on earth..? equivalence (==) not working

David Morris-Oliveros-2
I believe it does coerce types, but only on the C side, so if you're
using the API, and there's a number there, and you call lua_tostring(),
it will coerce it. I believe that's true for all types except for
booleans, which have no real mappings to other types.

// David Morris-Oliveros
// Camera & Lua Coder
// [hidden email]



Damian Stewart wrote:
> David Morris-Oliveros wrote:
>> What about the types?
>> Does 235 == '235' ?
>
> aah. it seems id is a string but arm_id is a number.
>
> i thought lua converted automagically between the two types..?
>
Reply | Threaded
Open this post in threaded view
|

Re: what on earth..? equivalence (==) not working

frey
In reply to this post by frey
Damian Stewart wrote:
> aah. it seems id is a string but arm_id is a number.
>
> i thought lua converted automagically between the two types..?

oh. i should rtfm a little more.. ;-)

--
f r e y
live music with computers
http://www.frey.co.nz

Reply | Threaded
Open this post in threaded view
|

Re: what on earth..? equivalence (==) not working

Mildred Ki'Lya
In reply to this post by frey

On Wed, 17 May 2006 16:55:18 +1200 Damian Stewart wrote:
> David Morris-Oliveros wrote:
> > What about the types?
> > Does 235 == '235' ?
>
> aah. it seems id is a string but arm_id is a number.
>
> i thought lua converted automagically between the two types..?
>

Not evrywhere ... Just when you clearly want a string (cancatenation).
And it's better like that, or else lua would need the operator === like
php :(

you can always convert string to numbers with tonumber() and number to
string with tostring().

Mildred
--
Mildred       <xmpp:[hidden email]> <http://mildred632.free.fr/>
Clef GPG :    <hkp://pgp.mit.edu> ou <http://mildred632.free.fr/gpg_key>
Fingerprint : 197C A7E6 645B 4299 6D37 684B 6F9D A8D6 [9A7D 2E2B]
Reply | Threaded
Open this post in threaded view
|

Re: what on earth..? equivalence (==) not working

Jose Luis Hidalgo Valiño
In reply to this post by frey
you can force the conversion with :

$ lua
Lua 5.1  Copyright (C) 1994-2006 Lua.org, PUC-Rio
 > =(235=="235"+0)
true

appending a +0 to a string  will force the conversion to a number.

--
    PpluX

El 17/05/2006, a las 7:02, Damian Stewart escribió:

> Damian Stewart wrote:
>> aah. it seems id is a string but arm_id is a number.
>> i thought lua converted automagically between the two types..?
>
> oh. i should rtfm a little more.. ;-)
>
> --
> f r e y
> live music with computers
> http://www.frey.co.nz
>