in reply to Re: How do I empty out a hash?
in thread How do I empty out a hash?

If you really wanted to iterate over the hash, using katie's method, there is no point in fetching the VALUE, only to throw it away. Hence, I'd code it thus:
#!/usr/bin/perl -w use strict; my %hash = ( Carol => 22, Mary => 21, Chris => 30 ); for (keys %hash){ delete $hash{$_}; };
which is a little easier on the eye ...

Replies are listed 'Best First'.
Re^2: Answer: How do I empty out a hash?
by Aristotle (Chancellor) on Aug 16, 2003 at 18:07 UTC
    That builds a list of keys up front - may or may not be a bad idea. If it is, I use each in scalar context to get something similar looking:
    my $k; delete $hash{$k} while $k = each %hash;
    (Incidentally, I'd write your version like so:)
    delete $hash{$_} for keys %hash;

    Makeshifts last the longest.