Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Answer: How do I empty out a hash?

by NetWallah (Canon)
on Aug 16, 2003 at 04:52 UTC ( [id://284312]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-19 18:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found