Re: any replacements for smartmatch on comparing two arrays
by Discipulus (Canon) on Sep 24, 2015 at 06:48 UTC
|
I found tobyink's reply on another forum (oh well is in the official docs..)
no if ($] >= 5.018), 'warnings' => 'experimental';
#or simply
no warnings 'experimental';
#or with more precision
no warnings 'experimental::smartmatch';
see also official docs perldelta and A little nicer way to use smartmatch on perl 5.18 and Experimental features now warn (reaching back to v5.10)
HtH
L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
| [reply] [d/l] |
|
|
Well, you can suppress the warnings, as shown by Discipulus, e.g. with the pragma:
no warnings 'experimental::smartmatch';
However, you should IMHO carefully consider whether you really want to do that. The smart match was for a time a full-fledged feature of Perl 5 and has been "downgraded" to "experimental" with Perl 5.20 if I remember correctly. This implies that it could be strongly modified or even removed in a future version, meaning that your program might cease to work after a Perl upgrade. Frankly, I would not use that feature for any program that is not a one-off script and might be used in the future.
Update: Sorry for a posting mistake: this post was meant as an answer to the OP, not to Discipulus's post. Too late, I am afraid, to do anything about it.
| [reply] [d/l] |
|
|
| [reply] [d/l] |
Re: any replacements for smartmatch on comparing two arrays
by Athanasius (Archbishop) on Sep 24, 2015 at 08:14 UTC
|
#! perl
use strict;
use warnings;
use List::Util qw( pairfirst );
use List::MoreUtils qw( mesh );
my @array1 = qw( a e i o u );
my @array2 = qw( a e i o u );
my $found = @array1 == @array2 && ! pairfirst { $a ne $b } mesh @arra
+y1, @array2;
print $found ? 'Match' : 'No match', "\n";
Output:
18:12 >perl 1380_SoPW.pl
Match
18:12 >
Hope that helps,
Update (13 Aug 2020): Fixed broken link.
| [reply] [d/l] [select] |
Re: any replacements for smartmatch on comparing two arrays
by Laurent_R (Canon) on Sep 24, 2015 at 08:26 UTC
|
In addition to my previous post on caveats about the smart match operator, it just occurred to me that I left part of your question unanswered. You might consider the List::Compare module, which does probably exactly what you need.
| [reply] |
Re: any replacements for smartmatch on comparing two arrays
by stevieb (Canon) on Sep 24, 2015 at 13:56 UTC
|
In addition to the previous suggestions including Laurent_R's suggestion of List::Compare, I've found that Data::Compare works quite well at this too:
perl -wMstrict -MData::Compare -E 'my @a=qw(1 2 a b); my @b=qw(1 2 a b
+); say Compare(\@a, \@b);'
1
perl -wMstrict -MData::Compare -E 'my @a=qw(1 3 a b); my @b=qw(1 2 a b
+); say Compare(\@a, \@b);'
0
| [reply] [d/l] [select] |
|
|
| [reply] |
|
|
You can always "inspire" yourself by looking at the source of List::Compare or Data::Compare and then include their code bodily into your script.
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James My blog: Imperial Deltronics
| [reply] |
|
|
|
|
my $match = @array1 == @array2 && !grep { !$_ } map { $array1[$_] eq $
+array2[$_] } 0 .. $#array1;
Hope that helps,
| [reply] [d/l] |