Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: Equality operators

by jkva (Chaplain)
on Apr 25, 2006 at 11:41 UTC ( #545538=note: print w/ replies, xml ) Need Help??


in reply to Re: Equality operators
in thread Equality operators

Mantadin, thank you. I am aware of this, however I am wondering why '1' == '1foo' evals to true while '1' eq 'foo1' does not.


Comment on Re^2: Equality operators
Select or Download Code
Re^3: Equality operators
by davorg (Chancellor) on Apr 25, 2006 at 11:44 UTC

    When converting strings to numbers, Perl only looks at the start of the string. '1foo' becomes 1 and 'foo1' becomes 0.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re^3: Equality operators
by salva (Abbot) on Apr 25, 2006 at 11:46 UTC
    because strings are automatically converted to numbers when needed. '1foo' is converted to 1 and 'foo1' is converted to 0.
Re^3: Equality operators
by ikegami (Pope) on Apr 25, 2006 at 14:44 UTC

    Others have already explained how the conversion works, but if you want to see for yourself the number into which a string is coerced, add 0 to the string.

    print(0+'a', "\n"); # 0 print(0+'b', "\n"); # 0 print(0+'1', "\n"); # 1 print(0+'2', "\n"); # 2 print(0+'1foo', "\n"); # 1 print(0+'foo1', "\n"); # 0

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://545538]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (14)
As of 2015-07-03 12:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (53 votes), past polls