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

Hi, My question is this , i have an array of hashes

@array=({key1=>1,key2=>2},{key1=>1,key2=>3},{key1=>1,key2=>5});

is there a simple method to check if all the key1 are 1 in the above array other than looping the array

janitored by ybiC: Balanced <code> tags around snippet, as per Monastery convention

Replies are listed 'Best First'.
Re: Checking if all the key values are same in an Array
by deibyz (Hermit) on Apr 26, 2005 at 11:32 UTC
    grep is your friend! :

    use strict; use warnings; my @array=({key1=>1,key2=>2},{key1=>1,key2=>3},{key1=>1,key2=>5}); print "Yes!\n" unless grep { $_->{key1} != 1 } @array;

    Or, if the array is large, List::Util's first is even a better friend:

    #!/usr/bin/perl use strict; use warnings; use List::Util qw( first ); my @array=({key1=>1,key2=>2},{key1=>1,key2=>3},{key1=>1,key2=>5}); print "Yes!\n" unless first { $_->{key1} != 1 } @array;

    Update: Ok, I agree with holli, grep and (probably) first are loops, but not explicit loops, that was what maybe he was asking for. When I think in loops, I think in for, while and friends, and I see map, grep... more as "list transformers".

      grep is a loop. And I strongly assume (without looking at the code) that List::Util uses a loop internally too.


      holli, /regexed monk/
        Let's not be too pedantic. Unless there's some kind of obfuscation/puzzle involved, usually when people say things about not wanting a loop, they usually mean they don't want to break up their code by a loop statement. I think grep in an expression is highly likely to be the desired answer.
Re: Checking if all the key values are same in an Array
by salva (Canon) on Apr 26, 2005 at 11:35 UTC
    no... well, it depends on what you do understand by "looping":
    my $some_are_different = grep { $_->{key1} != 1 } @array;
Re: Checking if all the key values are same in an Array
by holli (Abbot) on Apr 26, 2005 at 11:32 UTC
    no.


    holli, /regexed monk/
      Would you concider this looping?
      #!/usr/bin/perl use strict; my @array = ({key1=>1, key2=>2, key3=>3}, {key1=>1, key2=>2, key3=>3}, + {key1=>1, key2=>2, key3=>3} ); my $amount = scalar(@array); if (!$array[0 .. $amount]->{key1}) { print "no\n" }
      Update holli is right. bah. this code just checks if key1 of the second element of the array is false. There is no loop.
      "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
        No. Your code checks if the value under "key1" of the second hashref in the array is false.

        Consider:
        #!/usr/bin/perl use strict; my @array = ( {key1=>0, key2=>2, key3=>3}, {key1=>1, key2=>2, key3=>3}, {key1=>0, key2=>2, key3=>3} ); my $amount = scalar(@array); if (!$array[0..$amount]->{key1}) { print "no\n" }
        which should print "no" but does not, and
        #!/usr/bin/perl use strict; my @array = ( {key1=>0, key2=>2, key3=>3}, {key1=>1, key2=>2, key3=>3}, {key1=>0, key2=>2, key3=>3} ); my $amount = scalar(@array); if (!$array[0..$amount]->{key1}) { print "no\n" }
        which should not print "no" but does.


        holli, /regexed monk/