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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|