in reply to passing hash refs
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); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: passing hash refs
by fredsnertz (Initiate) on Apr 08, 2009 at 15:58 UTC | |
by RMGir (Prior) on Apr 08, 2009 at 16:40 UTC | |
by GrandFather (Saint) on Apr 08, 2009 at 23:19 UTC |