user846969
user846969

Reputation:

Using one hash reference rather than two in Perl

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

Answers (3)

runrig
runrig

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

Neil Slater
Neil Slater

Reputation: 27207

Using each:

my %k = ( a => 1, b => 2, c => undef ); 
while ( my ($i, $j) = each %k ) { say $i unless $j };

Upvotes: 2

Slaven Rezic
Slaven Rezic

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

Related Questions