in reply to Delete a zero-valued hash element

Use
length delete ($hash{$key}) || print " There is some error : $! ";

update - Oops. A kyle states below, length doesn't work with zero length hash values. So, use

defined delete ($hash{$key}) || print " There is some error : $! ";

Ah.. well... I can see the next problem: what if you say $hash{$key} = undef? In that case defined(delete) will fail

perl -le '$h{""}=undef; print ">",defined delete $h{""},"<"' ><

since delete returns undef here. So, probably best advice is:

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^2: Delete a zero-valued hash element
by kyle (Abbot) on Oct 02, 2007 at 20:05 UTC

    That will tell you the length of the value deleted, not how many items were deleted.

    my %h = ( foo => 'foo123', bar => 'bar456', nak => '' ); print length delete $h{foo}, "\n"; print length delete $h{nak}, "\n"; print scalar @{[delete $h{bar}]}, "\n"; __END__ 6 0 1