ramachandrajr has asked for the wisdom of the Perl Monks concerning the following question:

hey guys i wanna do this:

@array1 ~~ @array2;#this works for me since it compares elements insid +e two arrays,but when i use this in cgi smartmatch triggers experimen +tal @array1 eq @array2;#this doesnt work cz eq or == compares the no.of el +ements

either i want some way to suppress experimental or find a work around and i dont want to do a double foreach loop for comparing two arrays

Replies are listed 'Best First'.
Re: any replacements for smartmatch on comparing two arrays
by Discipulus (Canon) on Sep 24, 2015 at 06:48 UTC
      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.

        Laurent has shown the difference between knowledge and wisdom.

        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.
Re: any replacements for smartmatch on comparing two arrays
by Athanasius (Archbishop) on Sep 24, 2015 at 08:14 UTC

    Hello ramachandrajr,

    See also perlfaq4. Here is one (not necessarily optimal) way, using 2 modules:

    #! 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.

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

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.
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

      well guys i am on a shared server whose admin doesn't allow new modules.

        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

        OK, this does the comparison in a single line, using no modules and no explicit loops:

        my $match = @array1 == @array2 && !grep { !$_ } map { $array1[$_] eq $ +array2[$_] } 0 .. $#array1;

        Hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,