in reply to any replacements for smartmatch on comparing two arrays

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

Replies are listed 'Best First'.
Re^2: any replacements for smartmatch on comparing two arrays
by ramachandrajr (Novice) on Sep 24, 2015 at 15:09 UTC

    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

        I just tested this with Data::Compare. To use the Compare function, all that is required is appending the Compare sub contents from the Data::Compare source into the script following this required code (note that both Carp and Scalar::Util have been in core since pre-5.8).

        use warnings; use strict; use Carp; use Scalar::Util; my (%been_there, %handler); print Compare([], [qw(1 2)]);

      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,