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

G'day nysus,

"Is there a good, more perlish way of pulling this off?"

In the absence of any information about wanting to capture the deleted keys, I'd probably just do this:

delete @ta{grep !defined $ta{$_}, keys %ta};

I'm not sure if the way you've constructed %ta is really what you intended; however, with Perl 5.30.0 I get no errors or warnings:

$ perl -Mstrict -Mwarnings -E ' my %args = (profile => "foo"); my %ta = %args{qw{profile user password}}; say "BEFORE:"; say for keys %ta; delete @ta{grep !defined $ta{$_}, keys %ta}; say "AFTER:"; say for keys %ta; ' BEFORE: profile password user AFTER: profile

— Ken

Replies are listed 'Best First'.
Re^2: Consise way to filter out keys with undef values from a hash slice?
by choroba (Cardinal) on Jun 02, 2020 at 09:21 UTC
    > with Perl 5.30.0 I get no errors or warnings

    5.20.0 Delta: New slice syntax.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        Then I've probably misunderstood the part of your post I quoted.

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]