You are looping through @array, but only using element 0. Perhaps you meant foreach $nbr (0..$#array) as the loop or fubar(\$_) as the body?
$array[0] and $array[1] are already hashrefs; when you have the \$array[$nbr] you end up passing a reference to a reference to a hash: probably not what you want. If you want a reference to a hash, just pass $array[$nbr]. If you want a copy of the keys and values of the hash (which is how your fubar is written), pass %{$array[$nbr]}.
In fubar, you assign to a hash from @_, but @_ won't have the needed list of keys and values. You have a prototype which will mess you up, even if you were passing a flattened hash to fubar like fubar(%hash) or fubar(%{some hash reference}). I have to echo theorbtwo's recommendation: don't use prototypes unless you know what they do. Learn them later, if you want, after many other things.
In summary, be consistent with one of these approaches:
Flattened hash (has to copy all the keys and values twice, but may sometimes be more convenient):
Reference to hash:sub fubar { my %hash = @_; print $hash{key1}, $hash{key2}; } ... fubar(%{$arrayofhashes[$index]});
For completeness, the prototype works this way (but don't use it!):sub fubar { my $hashref = shift; print $hashref->{key1}, $hashref->{key2}; } ... fubar($arrayofhashes[$index]);
sub fubar(%) { my $hashref = shift; print $hashref->{key1}, $hashref->{key2}; } ... fubar(%{$arrayofhashes[$index]});
In reply to Re: Passing Hash to Subroutine
by ysth
in thread Passing Hash to Subroutine
by mjmaresca
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |