Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: Shortcut operator for $a->{'b'}=$b if $b;

by halley (Prior)
on Sep 20, 2005 at 16:26 UTC ( #493503=note: print w/replies, xml ) Need Help??

in reply to Shortcut operator for $a->{'b'}=$b if $b;

No really swift code I can think of, which prevents the keys from being inserted. But if you can allow empty keys to exist briefly, you can audit them afterwards:
$a = { d=>1, e=>2, f=>3, g=>undef, h=>0, i=>4 }; delete @$a{ grep { not $a->{$_} } keys %$a };
Of course, that's just shifting the ugliness.

[ e d @ h a l l e y . c c ]

Replies are listed 'Best First'.
Re^2: Shortcut operator for $a->{'b'}=$b if $b;
by radiantmatrix (Parson) on Sep 20, 2005 at 17:35 UTC

    A little less ugly audit:

    for (keys %$a) { delete $a->{$_} unless $a->{$_} }

    Note: that should probably be unless defined $a->{$_} so we don't remove false keys, but I made the choice to mirror the OP's syntax.

    Larry Wall is Yoda: there is no try{} (ok, except in Perl6; way to ruin a joke, Larry! ;P)
    The Code that can be seen is not the true Code
    "In any sufficiently large group of people, most are idiots" - Kaa's Law

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://493503]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2017-08-21 05:26 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (317 votes). Check out past polls.