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

8 messages
Open this post in threaded view
|

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

 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
Open this post in threaded view
|

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

 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? >
Open this post in threaded view
|

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

 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.
Open this post in threaded view
|

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

 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
Open this post in threaded view
|

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

 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..? >
Open this post in threaded view
|

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

 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