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); }
In reply to Re: passing hash refs
by kennethk
in thread passing hash refs
by fredsnertz
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |