Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: OT: clearing a hash (was: TIMTOWTDI doesn't mean invent an outlandish approach (usually))

by Abigail-II (Bishop)
on Oct 13, 2003 at 10:46 UTC ( [id://298804]=note: print w/replies, xml ) Need Help??


in reply to OT: clearing a hash (was: TIMTOWTDI doesn't mean invent an outlandish approach (usually))
in thread TIMTOWTDI doesn't mean invent an outlandish approach (usually)

The '%hash = undef' is incorrect:
$ perl -wle '%hash = undef; print scalar %hash' Odd number of elements in hash assignment at -e line 1. Use of uninitialized value in list assignment at -e line 1. 1/8
It'll create a one element hash, whose key is the empty string, with an undefined value.

The last three solutions modify the hash while iterating over the keys, which should be a no-no. However, I just read that in the documentation of 'each' that deleting the item most recently returned by 'each()' is safe; this feature seems to have been added (or documented) in 5.6.1.

Abigail

  • Comment on Re: OT: clearing a hash (was: TIMTOWTDI doesn't mean invent an outlandish approach (usually))
  • Download Code

Replies are listed 'Best First'.
Re^2: OT: clearing a hash
by Aristotle (Chancellor) on Oct 13, 2003 at 11:04 UTC
    Ah, so that's why I was puzzled - the hint on each was there the first I wanted to know about deleting keys while iterating. But you said three of the following will not necessarily work:
    1. map { delete $hash{$_} } keys %hash;
    2. while (my ($k,$v) = each %hash) { delete $hash{$k} };
    3. for (keys %hash) { delete $hash{$_} };
    4. delete $hash{$_} for keys %hash;
    With the each solution being valid that's one down, two to go, and now I'm more confused than before.. of the remaining three solutions, the for loops are identical except for syntactical details. So they must either both be valid or both invalid; then the map version must be valid and the for ones not. But I can't see any reason to conclude that any of them (all four) are not guaranteed to work.

    Makeshifts last the longest.

      Number 1) is safe because in this case, a list of keys will be produced before the deletes are called. For 3) and 4), I though that 'for (keys %hash)' was special cased to iterate over the hash instead of producing a list (just like 'for (@array)' is special cased), but I can't find it in the documentation right now. Anyway, regardless whether it's special cased or not, 3) and 4) are fine as well, due to the exception of modifying a hash while iterating over it by deleting the last element returned by 'each' (since 'keys' and 'values' uses the same iterator).

      Abigail

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://298804]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-24 18:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found