Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: deleting key/value from hashref

by Zed_Lopez (Chaplain)
on Sep 24, 2004 at 18:28 UTC ( [id://393625]=note: print w/replies, xml ) Need Help??


in reply to deleting key/value from hashref

You can use Data::Dumper to assure yourself that delete is working.

use Data::Dumper; my $var = { '1234-567' => { 'key1' => 'stuff' , 'key2' => 'stuff', }, '1234-997' => { 'key1' => 'stuff' , 'key2' => 'stuff', } }; print Dumper($var); delete $var->{'1234-997'}; print Dumper($var);

produces

$VAR1 = { '1234-567' => { 'key2' => 'stuff', 'key1' => 'stuff' }, '1234-997' => { 'key2' => 'stuff', 'key1' => 'stuff' } }; $VAR1 = { '1234-567' => { 'key2' => 'stuff', 'key1' => 'stuff' } };

You're getting bitten by the fact if $var->{a} doesn't exists, then tests on $var->{a}->{b} will autovivify $var->{a} (though the same test on $var->{a} wouldn't.)

my $var = {}; print if exists $var->{a}; print Dumper($var); print if exists $var->{a}->{b}; print Dumper($var);

produces

$VAR1 = {}; $VAR1 = { 'a' => {} };

Replies are listed 'Best First'.
Re^2: deleting key/value from hashref
by geektron (Curate) on Sep 24, 2004 at 18:58 UTC
    I was using Data::Dumper, which is how I know the row was auto-vivifying.

    I wasn't using  exists() in the checks I had tried. i was using  defined ... which was wrong.

      defined is fine too.

      %hash = ( key1 => 'bla', key2 => 0, key3 => undef, ); print( exist($hash{'key0'})?1:0, "\n"); # 0 print(defined($hash{'key0'})?1:0, "\n"); # 0 print( $hash{'key0'} ?1:0, "\n"); # 0 print( exist($hash{'key1'})?1:0, "\n"); # 1 print(defined($hash{'key1'})?1:0, "\n"); # 1 print( $hash{'key1'} ?1:0, "\n"); # 1 # catch: print( exist($hash{'key2'})?1:0, "\n"); # 1 print(defined($hash{'key2'})?1:0, "\n"); # 1 print( $hash{'key2'} ?1:0, "\n"); # 0 # catch: print( exist($hash{'key3'})?1:0, "\n"); # 1 print(defined($hash{'key3'})?1:0, "\n"); # 0 print( $hash{'key3'} ?1:0, "\n"); # 0

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-04-18 02:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found