Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^3: Identical Arrays

by Marshall (Prior)
on Aug 26, 2012 at 05:30 UTC ( #989789=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Identical Arrays
in thread Identical Arrays

Well, indeed!

I've never used smart match in production code because as this example confirms, it is not as "smart" as one might think! I stand corrected about this. I guess you have to very smart to use the "smart match". Obviously, I am not that smart.


Comment on Re^3: Identical Arrays
Re^4: Identical Arrays
by davido (Archbishop) on Aug 26, 2012 at 06:14 UTC

    It's a can of worms. :)

    Here's what's happening:

    First, when you perform @arrayA ~~ @arrayB the action you get is actually similar to ($arrayA[0] ~~ $arrayB[0]) && ($arrayA[1] ~~ $arrayB[1]) && .... So you're really getting a chain of scalar to scalar smart-matches.

    Next we have to scan down the list to find something that fits the description of 'scalar ~~ scalar', while keeping in mind that the left-hand side looks like a string, and the right hand side looks like a number. Here's the obscure rule:

    Any ~~ Num: numeric equality like: Any == Num

    (~~ added for clarity)

    So you're comparing something like this: '1ringy-dingy2ringy-dingy' ~~ 1. The right-hand side is a number. So a numeric comparison will be performed. And what happens when you use "1ringy-dingy2ringy-dingy" as a number? Well, if a string starts with a number, Perl will use that number and drop the rest when performing numeric operations. So '1ringy-dingy2ringy-dingy' ~~ 1 is the same as '1ringy-dingy2ringy-dingy' == 1, which is the same as 1 == 1.


    Dave

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2014-09-17 05:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (58 votes), past polls