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

... filter out undefined values ... [emphasis added]

My take is that keys with undefined values (either because non-existent or undef) are to be filtered out. One way:

c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my %args = ('profile' => 'foo', 'xyzzy' => undef, 'status' => 0); my %ta = map { defined $args{$_} ? ($_ => $args{$_}) : () } qw(profile password xyzzy status) ; dd 'filtered', \%ta; dd 'original', \%args; " ("filtered", { profile => "foo", status => 0 }) ("original", { profile => "foo", status => 0, xyzzy => undef })


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: Consise way to filter out keys with undef values from a hash slice?
by LanX (Saint) on Jun 01, 2020 at 21:45 UTC
    Here a concise way for cleaning up afterwards:

     $h{$_} // delete $h{$_} for keys %h

    Unfortunately the even shorter approach doesn't work with deleting aliases

    $_ // delete $_ for values %h

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery