Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

passing hash refs

by fredsnertz (Initiate)
on Apr 08, 2009 at 15:39 UTC ( #756370=perlquestion: print w/replies, xml ) Need Help??
fredsnertz has asked for the wisdom of the Perl Monks concerning the following question:

Why doesn't this work and how do I fix it? The "actions" array elements don't seem to make it out of the subroutine. Thanks,
#!/usr/bin/perl $hash_ref=set_up(); foreach $key (keys %{$hash_ref}) { $val=$$hash_ref{$key}; if (ref($val)) { print "\ntotal \'$key\' = ", scalar @{$hash_ref{$key}}, "\n"; foreach $act (@{$hash_ref{$key}}) { print "\t$act\n"; } } else { print "$key - $val\n"; } } sub set_up { my (%hash,@array); @array=("feed","walk","pet","groom"); $hash{'one'}="cat"; $hash{'two'}="dog"; push(@{$hash{'actions'}},@array); return(\%hash); }

Replies are listed 'Best First'.
Re: passing hash refs
by kennethk (Abbot) on Apr 08, 2009 at 15:46 UTC

    You have been bitten by omitting use strict;use warnings. Specifically, on line 8 you say "scalar @{$hash_ref{$key}}" which is trying to access a non-existent hash named %hash_ref instead of a scalar named $hash_ref. You require the dereference operator to get that result; see perlreftut. You do the same on line 9.

    Adding strictures and fixing that mistake gives:

    #!/usr/bin/perl use strict; use warnings; my $hash_ref = set_up(); foreach my $key (keys %{$hash_ref}) { my $val = $$hash_ref{$key}; if (ref($val)) { print "\ntotal \'$key\' = ", scalar @{$hash_ref->{$key}}, "\n"; foreach my $act (@{$hash_ref->{$key}}) { print "\t$act\n"; } } else { print "$key - $val\n"; } } sub set_up { my (%hash,@array); @array=("feed","walk","pet","groom"); $hash{'one'}="cat"; $hash{'two'}="dog"; push(@{$hash{'actions'}},@array); return(\%hash); }

      Thank you so much for getting me an answer in 15 minutes to something I'd already spent hours on. You have inspired me to start using strict and warnings ... well my code is around 600 lines already and I should be able to wrap it up now ... I'll use those on my next project.
        I'll use those on my next project.

        Please reconsider. Use them now!

        Your maintenance challenges down the road will be much fewer AND simpler if you deal with the problems strict and warnings complain about now, while you still remember what your code means...

        (You should also add documentation and tests, seriously. But making things strict/warnings clean is a good start)


        Mike

        Bzzzt - wrong answer. Add strictures now. You've already seen the benefit - two errors detected in about 30 lines of code, so there could be about 38 more errors to find in the remaining 570 lines which is about another two weeks of possible debugging time saved! ;)


        True laziness is hard work
Re: passing hash refs
by ikegami (Pope) on Apr 08, 2009 at 15:51 UTC

    You wouldn't have had this problem had you not disabled strictures by omitting use strict;. Use use strict;!!!

    You did $$hash_ref{$key} in one place and $hash_ref{$key} in the other. The latter accesses the (non-existant) hash %hash_ref, not the hash referenced by $hash_ref.

    By the way, $hash_ref->{$key} is a clearer way or writing $$hash_ref{$key}.

    And since you already have $hash_ref->{$key} in $val, @{ $hash_ref->{$key} } can be written as @{$val} and @$val.

    Cleaned up code:

    #!/usr/bin/perl use strict; use warnings; my $hash_ref = set_up(); foreach my $key (keys %{$hash_ref}) { my $val = $hash_ref->{$key}; if (ref($val)) { print "total '$key' = ", scalar @{$val}, "\n"; foreach my $act (@{$val}) { print "\t$act\n"; } } else { print "$key - $val\n"; } } sub set_up { return { one => "cat", two => "dog", actions => [ "feed", "walk", "pet", "groom" ], }; }

    In case you don't know, Data::Dumper is your friend when you need to debug a data structure such as the one you are building.

    Update: Added code.

      Thank you! I will look into Data::Dumper.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2018-10-17 19:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...














    Results (97 votes). Check out past polls.

    Notices?