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

hi all,
I have an complex hash structure like this,
%hoh = { 'comp' => { 'cmd' => { 'tc1' => { 'platform_type' => 'all', 'testsuite_type' => 'all +', 'ccc_id' => '2', 'view' => 'private' }, 'tc2' => { 'platform_type' => 'all', 'testsuite_type' => 'all +', 'ccc_id' => '2', 'view' => 'private' } } } };
From this structure, how do i delete the key/value pair for "tc2" or "tc1"?

I tried using it like this, but seems not working properly
delete $hoh{'comp'}{'cmd'}{'tc2'};
thanks in advance

Replies are listed 'Best First'.
Re: delete from complex hash
by Aristotle (Chancellor) on Nov 16, 2005 at 10:59 UTC

    That’s because there’s nothing useful in your hash. You wrote:

    %hoh = { # ... };

    You are assigning a one-element list whose first element is an anonymous hash reference to the %hoh hash. The result:

    $ perl -MData::Dumper -e'%hoh = { foo => 1 }; print Dumper \%hoh' $VAR1 = { 'HASH(0x812f180)' => undef };

    If you had warnings enabled, Perl would have told you that something is wrong:

    $ perl -we'%hoh = { foo => 1 };' Reference found where even-sized list expected at -e line 1.

    For your own sanity, get in the habit of using strict and warnings, and pay attention to error messages. That will save you untold hours of stupid mistakes and silly bug headaches.

    What you actually want is this:

    %hoh = ( # ... );

    Once you have that, your delete will work as posted.

    Makeshifts last the longest.