You have several problems here.

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):

sub fubar { my %hash = @_; print $hash{key1}, $hash{key2}; } ... fubar(%{$arrayofhashes[$index]});
Reference to hash:
sub fubar { my $hashref = shift; print $hashref->{key1}, $hashref->{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]});

In reply to Re: Passing Hash to Subroutine by ysth
in thread Passing Hash to Subroutine by mjmaresca

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.