Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

add and delete to hash reference

by philosophia (Sexton)
on Jul 25, 2007 at 21:04 UTC ( #628795=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to add and delete to a hash reference
the following code is not working as I intended:
print "create hashref\n"; my $closurehash = $sth->fetchall_hashref("name"); foreach $key ( keys(%{$closurehash}) ) { print "$closurehash->{$key}->{'name'}\n"; } print "add to hashref\n"; $closurehash->{ "test" } = "test"; # hash ref foreach $key ( keys(%{$closurehash}) ) { print "$closurehash->{$key}->{'name'}\n"; } print "delete from hashref\n"; delete $closurehash->{"fake35"}; foreach $key ( keys(%{$closurehash}) ) { print "$closurehash->{$key}->{'name'}\n"; }
Output is:
create hashref fake35 add to hashref fake35 delete from hashref
I'm not sure why it is not adding to the hash reference correctly. What am I doing wrong? Thanks.

Replies are listed 'Best First'.
Re: add and delete to hash reference
by Joost (Canon) on Jul 25, 2007 at 21:19 UTC
    this is why it's a VERY good idea to use strict:

    (also note that it's a good idea to post self-contained code for relatively simple problems like this).

    use strict; my $key; print "create hashref\n"; my $closurehash = { some_key => { name => 'fake35' } }; foreach $key ( keys(%{$closurehash}) ) { print "$closurehash->{$key}->{'name'}\n"; } print "add to hashref\n"; $closurehash->{ "test" } = "test"; # hash ref foreach $key ( keys(%{$closurehash}) ) { print "$closurehash->{$key}->{'name'}\n"; } print "delete from hashref\n"; delete $closurehash->{"fake35"}; foreach $key ( keys(%{$closurehash}) ) { print "$closurehash->{$key}->{'name'}\n"; }
    output:
    create hashref fake35 add to hashref Can't use string ("test") as a HASH ref while "strict refs" in use at +test.pl line 12.
    In other words, "test" is NOT a hash ref. Or if it is, it's a symbolic reference to some hash called "test" that isn't mentioned in your code.

    If you use a real hash-ref like so:

    $closurehash->{ "test" } = { name => "test"}; # hash ref
    it'll work as expected.

Re: add and delete to hash reference
by GrandFather (Saint) on Jul 25, 2007 at 21:20 UTC

    You are treating a HoH as a hash. If you were using strictures (use strict; use warnings;) you would have received the error:

    Can't use string ("test") as a HASH ref while "strict refs" in use at +...

    Consider:

    use strict; use warnings; my $closurehash = {1 => {name => 'fake35'}}; print "create hashref\n"; foreach my $key ( keys(%{$closurehash}) ) { print "$closurehash->{$key}->{'name'}\n"; } print "add to hashref\n"; $closurehash->{ "test" } = "test"; # hash ref foreach my $key ( keys(%{$closurehash}) ) { print "$closurehash->{$key}->{'name'}\n"; } print "delete from hashref\n"; delete $closurehash->{"fake35"}; foreach my $key ( keys(%{$closurehash}) ) { print "$closurehash->{$key}->{'name'}\n"; }

    Prints:

    create hashref fake35 add to hashref Can't use string ("test") as a HASH ref while "strict refs" in use at +noname1.pl line 14.

    whereas:

    use strict; use warnings; my $closurehash = {1 => {name => 'fake35'}}; print "create hashref\n"; foreach my $key ( keys(%{$closurehash}) ) { print "$closurehash->{$key}->{'name'}\n"; } print "add to hashref\n"; $closurehash->{2}{ "name" } = "test"; # hash ref foreach my $key ( keys(%{$closurehash}) ) { print "$closurehash->{$key}->{'name'}\n"; } print "delete from hashref\n"; delete $closurehash->{1}; foreach my $key ( keys(%{$closurehash}) ) { print "$closurehash->{$key}->{'name'}\n"; }

    prints:

    create hashref fake35 add to hashref fake35 test delete from hashref test

    which is likely what you were after. Always use strictures!


    DWIM is Perl's answer to Gödel
Re: add and delete to hash reference
by fmerges (Chaplain) on Jul 25, 2007 at 21:16 UTC

    Hi,

    Add the following to your code, and see it for yourself:

    use Data::Dumper; print Dumper($closurehash);

    Regards,

    fmerges at irc.freenode.net
Re: add and delete to hash reference
by oxone (Friar) on Jul 25, 2007 at 21:22 UTC
    Hi. Your add is working fine, but you're trying to report on a subhash of what you just added, which doesn't exist. If you change your testing statement to this you'll see your 'test' key:
    print "$closurehash->{$key}\n";

    I think your mistake is to assume that ->{name} is some Perlish magic to tell you the name of something. It isn't - it's a reference to a key called 'name' in a subhash of your $closurehash->{$key}, which doesn't exist because you haven't created it.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2022-05-22 02:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (78 votes). Check out past polls.

    Notices?