Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using one hash reference rather than two in Perl

Tags:

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;

2 Answers

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;
like image 174
Slaven Rezic Avatar answered Mar 05 '26 16:03

Slaven Rezic


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.

like image 22
runrig Avatar answered Mar 05 '26 16:03

runrig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!