Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: Deleting undef values from a hash

by ysth (Canon)
on Dec 14, 2003 at 03:31 UTC ( #314609=note: print w/replies, xml ) Need Help??

in reply to Deleting undef values from a hash

Not all undefs are created equal:
#!/usr/bin/perl -w use strict; use warnings; our %foo; @foo{'foo','baz'} = (); for my $key ('foo','bar','baz') { print "key $key ", (\undef == \delete $foo{$key} ? "didn't exist" : "existed"), "\n"; }
Doesn't work for tied hashes or magic hashes such as %SIG or %ENV.

Replies are listed 'Best First'.
Re: Re: Deleting undef values from a hash
by tilly (Archbishop) on Dec 14, 2003 at 23:25 UTC
    I think that you'll have a hard time finding that documented. ;-) (Though I have a pretty good idea why it works.)

    I also wouldn't want to trust it across Perl versions. That seems very specific to perl5, and I would not plan on that specific optimization finding its way into Ponie for instance.

      It should be ok for perl5, though. The division of responsibilities between hv_delete_* and pp_delete pretty much require that it work. hv_delete_* will return null if the key didn't exist, which pp_delete will replace with the immortal undef value. If the key did exist, hv_delete_* will return the value (even undef) in a different sv.

      It's obviously better practice to check exists in the first place, outside of obfuscation and golf.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://314609]
[LanX]: Montabaur station
[erix]: (I used http://afstandmete )
LanX The stations of Limburg Süd and Montabaur, which are approximately 20 km apart, ...
[LanX]: teh route I get from gmaps is 1662 km long and is crossing the Swiss Alps (mounting 2400 m) ... I wouldn't try this in December...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (10)
As of 2017-12-13 15:32 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (369 votes). Check out past polls.