Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

How do I empty out a hash?

by vroom (His Eminence)
on Mar 02, 2000 at 23:36 UTC ( #4687=perlquestion: print w/replies, xml ) Need Help??

vroom has asked for the wisdom of the Perl Monks concerning the following question: (hashes)

How do I empty out a hash?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I empty out a hash?
by vroom (His Eminence) on Jan 21, 2000 at 01:36 UTC
    You can set it equal to an empty hash or undef it;
    %hash=(); undef %hash;
Re: How do I empty out a hash?
by arno (Scribe) on Oct 04, 2003 at 12:39 UTC

    To delete a key, you don't need to know her value.
    Here's my code :

    my %hash = ( Carol => 22, Mary => 21, Chris => 30 ); map { delete $hash{$_} } keys %hash;
    Update: Of course this makes use of map in void context. Yick. And a couple other suggestions came in by way of followup:

    • delete @hash{(keys %hash)}; # Hash slice
    • %hash = (); # Most efficient method.


      Easier yet.
      delete @hash{(keys %hash)};
      Update: this is, of course, only useful when you want to use delete the keys from one hash in another one, as in
      delete @foo{(keys %bar)};
      Otherwise, only the below solution makes any sense.

      Makeshifts last the longest.

        Even more easier!

        %hash = ();

        And twice more quicker also.

Re: How do I empty out a hash?
by DigitalKitty (Parson) on Aug 16, 2003 at 01:51 UTC

    You could also iterate over the hash and delete each key/value pair with this:
    #!/usr/bin/perl -w use strict; my %hash = ( Carol => 22, Mary => 21, Chris => 30 ); my( $k, $v); while(( $k, $v) = each %hash ) { delete $hash{$k}; }

    The previous solutions are more efficient but I thought you might appreciate an alternate method.

    Update: Several followups included these suggestions:

    • for (keys %hash) { delete $hash{$_};};
    • delete $hash{$_} for keys %hash;

    Both of those methods ignore the values, which are unimportant when deleting hash table entries.

    Hope this helps,

      The quick way to clear a hash is
      %hash = ();
      i.e. set the empty list as the hash. Even if the hash is tied, a properly implemented tieing module should implement both the DELETE and CLEAR functions, so it should work in that case as well.


      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 ...
        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.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2022-12-05 20:12 GMT
Find Nodes?
    Voting Booth?

    No recent polls found