Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Consise way to filter out keys with undef values from a hash slice?

by davido (Cardinal)
on Jun 02, 2020 at 14:59 UTC ( #11117601=note: print w/replies, xml ) Need Help??


in reply to Consise way to filter out keys with undef values from a hash slice?

So you want to filter-in hash elements that have a defined value:

my %args = (profile => 'foo'); my %ta = map {defined($args{$_} ? ($_ => $args{$_}) : ()} qw(profile u +ser password);

If a map iteration returns an empty list, nothing gets added to the recipient's list. I don't usually like making map behave like grep, but in this case it seems a reasonable fit.

grep behavior can be provided by map by adding a Boolean test and a conditional return value.

my @array = grep {COND} LIST; my @array = map {COND ? $_ : ()} LIST

Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11117601]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2021-01-23 05:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?