dol has asked for the wisdom of the Perl Monks concerning the following question:
Hello all!
Caveat: I'm fairly new to Perl so the answer may be obvious.
I'm trying to delete a key in a hash-of-hashes using references. I wrote some mock-up code isolating the issue:
#!/usr/bin/perl -w use strict; my %HoH = ( flintstones => { lead => "fred", pal => "barney", }, jetsons => { lead => "george", wife => "jane", "his boy" => "elroy", }, ); my $href1 = \%{$HoH{flintstones}}; my $href2 = $HoH{flintstones}; my $href3 = \%HoH; print "\$HoH{flintstones}: $HoH{flintstones}\n\$href1: $href1\n\$href2 +: $href2\n\$href3->{flintstones}: ",$href3->{flintstones},"\n"; #Works delete($HoH{flintstones}); #delete($href3->{flintstones}); #Fails #delete($href1); #delete($href2);
Output:
$ ./hashreftest2.pl $HoH{flintstones}: HASH(0x7ce220) $href1: HASH(0x7ce220) $href2: HASH(0x7ce220) $href3->{flintstones}: HASH(0x7ce220)
From the output all four variables look equivalent to me. But any of the last two commented delete lines (i.e. "delete($href1);" and "delete($href2);") fails with this error:
delete argument is not a HASH or ARRAY element or slice at ./hashreftest2.pl line 29.I have searched the web but still can't understand why this happens. Any help is appreciated. In case it matters I'm running Perl v5.12.3 under Linux.
Thank you!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Deleting from Hash-of-Hash using references
by JavaFan (Canon) on Nov 26, 2011 at 23:07 UTC | |
by dol (Novice) on Nov 27, 2011 at 00:01 UTC | |
|
Re: Deleting from Hash-of-Hash using references
by Marshall (Canon) on Nov 27, 2011 at 09:34 UTC | |
by dol (Novice) on Nov 28, 2011 at 23:12 UTC |