Stephen Toney has asked for the wisdom of the Perl Monks concerning the following question:

Dear experts:

I have a reference to a hash-of-hashes and need to delete from it. Deleting a leaf node is no problem:
delete ($HoHref->{$element}{$attr});
but deleting an entire element does nothing and returns no warning or error:
delete ($HoHref->{$element});
I don't think the leaf nodes have to be removed first, but in trying to do so, I found that this does nothing either:
$HoHref->{$element} = ();
Any clues would be most welcome! Thanks in advance,

Stephen

Janitored by Arunbear - replaced pre tags with code tags, to allow code extraction

Replies are listed 'Best First'.
Re: delete from HoH ref
by dave_the_m (Monsignor) on Apr 10, 2005 at 14:39 UTC
    Works for me:
    $ cat /tmp/p my $HoHref = { a => { p => 1 }, b => { q => 1 } }; use Data::Dumper; print Dumper $HoHref; delete $HoHref->{a}; print Dumper $HoHref; $ $ perl /tmp/p $VAR1 = { 'a' => { 'p' => 1 }, 'b' => { 'q' => 1 } }; $VAR1 = { 'b' => { 'q' => 1 } };
    You'll need to give more details, such as a small (but complete) sample script, for us to help you further.

    Dave.

      Thanks for the reply. I looked harder at my code and realized the delete was working but I had a control-flow problem!

      Thank you, Stephen
Re: delete from HoH ref
by holli (Abbot) on Apr 10, 2005 at 14:40 UTC
    use strict; use warnings; use Data::Dumper; my $h = { a => { c=>"b" }, A => { C=>"B" }, }; delete $h->{a}{c}; delete $h->{A}; print Dumper ($h);
    prints
    $VAR1 = { 'a' => {} };
    as expected. You might want to show us some of your code.


    holli, /regexed monk/
Re: delete from HoH ref
by borisz (Canon) on Apr 10, 2005 at 14:45 UTC
    delete ($HoHref->{$element});
    should work as expected.
    use Data::Dumper; my $href = { elem1 => { attr => 12, attr2 => 44 }, elem2 => { attr => 12, attr2 => 44 } }; delete $href->{elem1}; print Dumper($href); __OUTPUT__ $VAR1 = { 'elem2' => { 'attr2' => 44, 'attr' => 12 } };
    Boris
Re: delete from HoH ref
by Zaxo (Archbishop) on Apr 10, 2005 at 14:42 UTC

    Are you sure $element is a key in %$HoHref ? What you're doing does work:

    $ perl -e'my $hohr = {foo=>{},bar=>{},baz=>{}};print delete ($hohr->{b +az}), $/;print keys %$hohr' HASH(0x804b5c0) barfoo$
    Perhaps you have unchomped newlines in your keys or $element? That could explain why your keys aren't found.

    After Compline,
    Zaxo

Re: delete from HoH ref
by sh1tn (Priest) on Apr 10, 2005 at 14:44 UTC
    Please, show the output of:
    use Data::Dumper; print Dumper($HoHref);


      Before *and* after the delete, preferably.