Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: how to use "delete" with a hash of hash

by Athanasius (Abbot)
on Jan 29, 2013 at 02:32 UTC ( #1015790=note: print w/ replies, xml ) Need Help??


in reply to how to use "delete" with a hash of hash

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,


Comment on Re: how to use "delete" with a hash of hash
Select or Download Code
Replies are listed 'Best First'.
Re^2: how to use "delete" with a hash of hash
by brassbin (Novice) on Jan 29, 2013 at 05:10 UTC

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1015790]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2015-07-08 07:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (96 votes), past polls