Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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
    Hi,

    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.

    Arnaud

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

    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,
    -Katie

      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.

      Liz

      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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (1)
As of 2022-11-26 18:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?