Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Perl-Sensitive Sunglasses
 
PerlMonks  

How do I empty out a hash?

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

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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 20, 2000 at 20: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 DigitalKitty (Parson) on Aug 15, 2003 at 21: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

      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.

      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

Re: How do I empty out a hash?
by arno (Scribe) on Oct 04, 2003 at 08: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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://4687]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.