cancel
Showing results for 
Search instead for 
Did you mean: 

Perl: array elements

 
SOLVED
Go to solution
Highlighted
Kalin Evtimov
Regular Advisor

Perl: array elements

Hi!
I have a small problem:

How do I check all elements of an array whether thay are the same (for example all elements are true and then I make some output, and if only one is false I break it)

Thank you!
4 REPLIES 4
Highlighted
Arunvijai_4
Honored Contributor
Solution

Re: Perl: array elements

Hi, You can use Array::Compare perl module do this,

http://search.cpan.org/~davecross/Array-Compare-1.13/lib/Array/Compare.pm

my @arr1 = 0 .. 10;
my @arr2 = 0 .. 10;

my $comp = Array::Compare->new;

if ($comp->compare(\@arr1, \@arr2)) {
print "Arrays are the same\n";
} else {
print "Arrays are different\n";
}

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Highlighted
Kalin Evtimov
Regular Advisor

Re: Perl: array elements

This will work, of course, but I need a script with no additional modules used in it. I may have a look at the implementation of that module.
10x a lot!
Highlighted
H.Merijn Brand (procura
Honored Contributor

Re: Perl: array elements

There are of course two different interpretations of array's are equal:

The actual content of (1..10) is the same as (5..10,1..4), but the match would fail.
Furthermore, you could use Test::More instead of Array::Compare, as Test::More is a core module, and you don't have to install extra modules:

lt09:/home/merijn 104 > perl -MTest::More=no_plan -le'@a=1..10;@b=5..9,1..4,10;is_deeply(\@a,\@b,"Array contents match")'
not ok 1 - Array contents match
# Failed test 'Array contents match'
# in -e at line 1.
# Structures begin differing at:
# $got->[0] = '1'
# $expected->[0] = '5'
1..1
# Looks like you failed 1 test of 1.
Exit 1
lt09:/home/merijn 105 > perl -MTest::More=no_plan -le'@a=1..10;@b=1..10;is_deeply(\@a,\@b,"Array contents match")'
ok 1 - Array contents match
1..1
lt09:/home/merijn 106 >

But you can of course also sort, but note that default sorting is alphanumeric

lt09:/home/merijn 106 > perl -MTest::More=no_plan -le'@a=1..10;@b=sort 5..9,1..4,10;is_deeply(\@a,\@b,"Array contents match")'
not ok 1 - Array contents match
# Failed test 'Array contents match'
# in -e at line 1.
# Structures begin differing at:
# $got->[1] = '2'
# $expected->[1] = '10'
1..1
# Looks like you failed 1 test of 1.
Exit 1
lt09:/home/merijn 107 > perl -MTest::More=no_plan -le'@a=1..10;@b=sort{$a<=>$b}5..9,1..4,10;is_deeply(\@a,\@b,"Array contents match")'
ok 1 - Array contents match
1..1
lt09:/home/merijn 108 >

Best thing about is_deeply () is that it also accepts hashes and hashes of hashes, lists of hashes, hashes of lists of lists of hashes of lists ...

If you just want to test the array's, Test::More also provides eq_array (), and friends, so:

lt09:/home/merijn 115 > perl -MTest::More=no_plan -le'@a=1..10;@b=1..10;ok print"Arrays ",eq_array(\@a,\@b)?"do":"dont"," match";'
Arrays do match
ok 1
1..1
lt09:/home/merijn 116 > perl -MTest::More=no_plan -le'@a=1..10;@b=5..10,1..4;ok print"Arrays ",eq_array(\@a,\@b)?"do":"dont"," match";'
Arrays dont match
ok 1
1..1
lt09:/home/merijn 117 >

But for you, the best function would be eq_set ()

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Highlighted
H.Merijn Brand (procura
Honored Contributor

Re: Perl: array elements

I noted that I missed some parens.

@b = (5..9,2..4,10,1);

Now for the full demo:

-->8---
#!/pro/bin/perl

use strict;
use warnings;

use Test::More 'no_plan';

my @a = (1 .. 10);
my @b = (5 .. 9, 2 .. 4, 10, 1);

isnt ("@a", "@b", "Plain in interpolation");

ok (!eq_array (\@a, \@b), "eq_array");
ok ( eq_array ([sort @a], [sort @b]), "eq_array on sorted ref");
is_deeply ([sort @a], [sort @b], "is_deeply on sorted ref");
ok ( eq_array ([sort { $a <=> $b } @a], [sort { $a <=> $b } @b]), "eq_array on num-sorted ref");
is_deeply ([sort { $a <=> $b } @a], [sort { $a <=> $b } @b], "is_deeply on num-sorted ref");

eq_set (\@a, \@b, "eq_set");
-->8---

pc09:/home/merijn 105 > perl xx.pl
ok 1 - Plain in interpolation
ok 2 - eq_array
ok 3 - eq_array on sorted ref
ok 4 - is_deeply on sorted ref
ok 5 - eq_array on num-sorted ref
ok 6 - is_deeply on num-sorted ref
1..6
pc09:/home/merijn 106 >

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn