Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
perldoc -f delete states:
Returns each element so deleted or the undefined value if there was no such element.
Which is of course nice, because it gives you a way to find out whether a key was really deleted from the hash. Or does it?

If the value associated with the key in the hash was undef, then being returned undef doesn't tell you anything. The undef could be from the hash, it could also be supplied Perl to "indicate" nothing was deleted from the hash.

So I decided to see whether deleting in array/list context would yield meaningful information in that respect. It doesn't:

my %a = (foo => undef); print "existed = ".(() = delete $a{foo})."\n"; print "notexisted = ".(() = delete $a{bar})."\n"; __END__ existed = 1 notexisted = 1

It seems that Perl is returning an array with the result of all possible deletions, so an array with the number of keys attempted to be deleted, not the actual number of keys deleted. Observe:

my %a = (foo => 1, bar => 2, baz => 3); $" = ','; print "existed = @{[delete $a{foo}]}\n"; print "notexisted = @{[delete @a{qw(foo bar baz)}]}\n"; __END__ existed = 1 notexisted = ,2,3

Anyway, that's what I learned today.

Since this behaviour goes back to at least Perl 5.00503, I think I'll provide a documentation patch, so that at least I will understand this the next time I read it.


Just to be clear: I knew about exists() ;-) My reason for using undef as a value in the hash, is that it uses less memory than a defined value:

use Devel::Size qw(size total_size); $a{1} = undef; $b{1} = 0; $c{1} = 1; $d{1} = 10; $e{1} = 'abcd'; print "$_: ".(total_size( \%{$_} ) - size( \%{$_} ))."\n" for a..e; __END__ a: 12 b: 16 c: 16 d: 16 e: 29
This is still a lot more than I would have hoped ;-( Especially if you have millions of keys.

In reply to Deleting undef values from a hash by liz

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [marto]: Corion, I'll talk to you later, but that are is generally fine :)
    [hippo]: Just not sure that the expense and hassle is worth the benefit.
    [LanX]: expense? .... meeting ppl is inspiring ...
    [LanX]: ... and it might be your last chance ... I heard Perl is dead! OO
    [marto]: someone read the post about search.cpan being shut down, and commented "It's going to be awhile before I learn to not type "" into my address bar."

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (11)
    As of 2018-06-20 09:39 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (116 votes). Check out past polls.