Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Identical Arrays

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


in reply to Identical Arrays

The Perl 5.10 "smart match" is a complex critter and what it does for complex data structures is hard to understand. But for simple array's, it works.

#/usr/bin/perl -w use strict; use v5.10.0; my @x = (1, 2, 3); my @y = (2, 3, 1); my @z = (1, 2, 4); print "x and y are equivalent" if sort (@x) ~~ sort (@y); # the sort order (numeric or alpha) doesn't matter # as long as it is consistent. __END__ x and y are equivalent
Oh, don't use either a or b for user Perl variables. In this case, it is ok, but $a and $b are special variables reserved for sort() and other uses. Unlike many other languages, $a is distinct from @a... the same name can be used for different variable types. However, "a" and "b" are so special that I cannot recommend that.

Update:
I see that I got a "down vote", well ok. Tell us what is wrong about this? I tend to stay away from this "smart match" thing, but it does work and is easy to understand for simple data structures - what it does for more complex structure IS hard to understand. The Perl sort algorithm works just fine for relatively small numbers of items.


Comment on Re: Identical Arrays
Download Code
Re^2: Identical Arrays
by Anonymous Monk on Aug 26, 2012 at 05:07 UTC
    # the sort order (numeric or alpha) doesn't matter # as long as it is consistent.
    my @x = ('1ringy-dingy2ringy-dingy', 2, 3); my @y = (2, 3, 1);
      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.

        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://989785]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2014-07-29 13:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (217 votes), past polls