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
|