in reply to How can I delete a key' value from a hash
For what value of "does not work"? Could it be that deleting the city then revivfying it by "deleting" the (non-existant) ZIP field is causing you grief? Consider:
use strict; use warnings; use Data::Dump::Streamer; my $location_hash = { 'New York' => {ZIP => '123-456-789'}, 'Dunedin' => {ZIP => '9001'} }; for my $city ( keys %$location_hash ) { print "$city $location_hash->{ $city }->{ZIP}\n"; delete ($location_hash->{ $city }); delete ($location_hash->{ $city }->{ 'ZIP' } ); } Dump $location_hash;
Prints:
Dunedin 9001 New York 123-456-789 $HASH1 = { Dunedin => {}, "New York" => {} };
You will note that the process of referencing the ZIP key in the second delete autovivifies the city key that you had just deleted. The first delete works and does everything required. The second delete is not required and is likely what is causing you confusion.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How can I delete a key' value from a hash
by kevyt (Scribe) on Jan 08, 2007 at 05:05 UTC |