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!


In reply to Deleting from Hash-of-Hash using references by dol

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.