- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Perl: array elements
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-12-2006 07:27 PM
тАО01-12-2006 07:27 PM
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!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-12-2006 07:44 PM
тАО01-12-2006 07:44 PM
Solutionhttp://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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-12-2006 07:48 PM
тАО01-12-2006 07:48 PM
Re: Perl: array elements
10x a lot!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-12-2006 08:22 PM
тАО01-12-2006 08:22 PM
Re: Perl: array elements
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-12-2006 10:37 PM
тАО01-12-2006 10:37 PM
Re: Perl: array elements
@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