DelphiFAQ Home Search:

How to delete entries from a hash (associative array) in perl

 

comments5 comments. Current rating: 3 stars (3 votes). Leave comments and/ or rate it.

Question:

I need to remove some elements of my hash in perl. Using undef seems not to work.
How do I do it other than copying every valid element into a target hash?

Answer:

Indeed undef will not work.

See the snippets below for a solution of your problem. It involves delete

my %months = (
  'Jan' => 31,
  'Feb' => 28,
  'Mar' => 31);

# this kills the whole hash
undef %months;

# this does NOT work
undef $months{'Feb'};

# this does not work either
$months{'Feb'} = undef;

# this deletes entry 'Feb':
delete $months{'Feb'};

# this is equivalent to deleting the whole hash:
foreach $key (keys %months) {
  delete $months{$key};
}

# this will also delete all keys:
delete @months{keys %months}

# another way to empty the hash
%months = ();

Content-type: text/html

Comments:

2012-11-12, 02:18:13
anonymous from United Kingdom  
2013-06-27, 19:14:10
anonymous  
rating
2013-06-27, 19:30:08
[hidden]  
rating
my %months = (JUNE
'Jan' => 31,
'Feb' => 28,
'Mar' => 31);

# this kills the whole hash
undef %months;JUNE

# this does NOT work
undef $months{JUNE};

# this does not work either
$months{'JUNE} = undef;

# this deletes entry 'JNUNE':
delete $months{'JUNE'};

# this is equivalent to deleting the whole hash:
foreach $key (keys %months) {
delete $months{$key};
}

# this will also delete all keys:
delete @months{keys %months}

# another way to empty the hash
%months = (JUNE);
2013-06-30, 18:41:06
anonymous from Australia  
rating
To all that HOW read about this girl she is not a saecmmer
she is the LOVE of MY Live.
Is web got me so mad. that i show her photo on here. and so i post her photo too but i woos not think and i did IT. I wish i can tack it off OR DELETE the one i post it BUT i don't know how. SO PLEASE Don't believe what you read about she girl she DO LOVE ME. She is the LOVE of my LIVE I LOVE HER.
PS: IF YOU KNOW HOW TO DELETE IT PLEASE I BEG YOU LET ME KNOW. ON toseeme2@gmail.com.
PLEASE GUY THINK BEFORE YOU POST ANY PHOTO ON THIS WEB.
This image was also posted here:
Dating scammer Carolina Douglas
Dating scammer Carolina Douglas
How to delete entries from a hash (associative array) in perl



Keywords:
2013-06-30, 18:47:51
anonymous: THIS GIRL IS NOT A SACMMRE. from Australia  
y %months = (JUNE

# this kills the whole hash
undef %months;(JUNE)

# this does NOT work
undef $months{'JUNE'};

# this does not work either
$months{'JUNE'} = undef;

# this deletes entry 'Feb':
delete $months{'JUNE'};

# this is equivalent to deleting the whole hash:
foreach $key (keys %months) {JUNE
delete $months{$key};
}

# this will also delete all keys:
delete @months{keys %months}

# another way to empty the hash
%months = (JUNE);

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: