in reply to Checking if all the key values are same in an Array

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

Replies are listed 'Best First'.
Re^2: Checking if all the key values are same in an Array
by holli (Abbot) on Apr 26, 2005 at 11:34 UTC
    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.