Reputation:
Is there some way in Perl that I can mention k just once on the second line:
my %k = (a=>1, b=>2, c=>undef);
say for grep{!$k{$_}} keys %k;
Upvotes: 3
Views: 120
Reputation: 6524
Yes:
$b or say $a while ($a,$b) = each %k
But that's not any better (worse, IMO), so I'd stick with what you have.
Upvotes: 4
Reputation: 27207
Using each
:
my %k = ( a => 1, b => 2, c => undef );
while ( my ($i, $j) = each %k ) { say $i unless $j };
Upvotes: 2
Reputation: 4581
Make use of mapp
and grepp
as found in the CPAN module List::Pairwise
:
use List::Pairwise qw(grepp mapp);
my %k = (a=>1, b=>2, c=>undef);
say for mapp { $a } grepp { !$b } %k;
Upvotes: 4