Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

how to use "delete" with a hash of hash

by brassbin (Novice)
on Jan 29, 2013 at 00:13 UTC ( #1015779=perlquestion: print w/replies, xml ) Need Help??
brassbin has asked for the wisdom of the Perl Monks concerning the following question:


i have a hash of hash like this

%hoh = ( 'item1' => { 'key1' => '1', 'key2' => '1', 'key3' => '0' }, 'item2' => { 'key1' => '0', 'key2' => '1' } )

this call of delete

delete $hoh{$item2}

deletes the sub-level hash but it does not delete the top level key "item2" itself, how do i delete the key "item2" itself after deleting the sub hash below it?

Thank you very much!

Replies are listed 'Best First'.
Re: how to use "delete" with a hash of hash
by smls (Friar) on Jan 29, 2013 at 00:36 UTC


      delete $hoh{item2};


      delete $hoh{$item2};

    and it does do what you request - it deletes the key "item2" from the top-level hash.

Re: how to use "delete" with a hash of hash
by Athanasius (Chancellor) on Jan 29, 2013 at 02:32 UTC

    Hello brassbin,

    smls++ has answered your question, but it occurs to me: if you don’t have a variable named $item2, either use strict or use warnings would have shown the error. With warnings:

    Name "main::item2" used only once: possible typo at...

    and with strict:

    Global symbol "$item2" requires explicit package name at...

    Moral: Always begin each script with:

    use strict; use warnings;

    It’s often useful to have:

    use diagnostics;

    as well, to clarify any warnings or error messages.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      thank you both! but i just realized something, i actually meant to say i used the "delete" call this way

      $a = "item2"; delete $hoh{$a};

      it deleted the sub-level hash but not the key "item2" itself

        Please post a piece of code that demonstrates the problem, including expected result and actual result. I could not reproduce the issue from your problem description.

        use Data::Dumper; %hoh = ( 'item1' => { 'key1' => '1', 'key2' => '1', 'key3' => '0' }, 'item2' => { 'key1' => '0', 'key2' => '1' } ); print "Before delete:\n"; print Dumper \%hoh; my $a = "item2"; delete $hoh{$a}; print "\n\nAfter delete:\n"; print Dumper \%hoh; print "$a deleted!" if not exists $hoh{$a};
        Before delete: $VAR1 = { 'item1' => { 'key2' => '1', 'key1' => '1', 'key3' => '0' }, 'item2' => { 'key2' => '1', 'key1' => '0' } }; After delete: $VAR1 = { 'item1' => { 'key2' => '1', 'key1' => '1', 'key3' => '0' } }; item2 deleted!

        That being said, you should abstain from using $a and $b as variables, as they have special meaning to the sort function.

Re: how to use "delete" with a hash of hash
by Rahul6990 (Beadle) on Jan 29, 2013 at 06:53 UTC
    The code posted by muba is working correctly. I don't know what is the problem with your code. You can use his.

      thanks for pointing that out, i think i know now where the problem is

      i was using a simplified code snippet to highlight the top-level key deletion question, the actual code is a bit more complicated, the variable that holds the top-level key is the result of a regular expression match, and i yet again underestimated the greediness of the .* construct

      thanks again for pointing out the useful of "warnings" & "diagnostics" pragmas, and your help on the usage of the "delete" function in the HoH context, thank you all

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1015779]
Approved by Illuminatus
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2018-06-19 13:40 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (114 votes). Check out past polls.