First, there's the problem that you're passing non-existent variables $HoA1 and $HoA2 to the sub. (Yay for using use strict;!)

You want to pass the hashes, but you can't do that. You can only pass scalars as arguments. So let's pass a reference to the hash instead.

test(\%HoA1, \%HoA2, ...);

Then there's the problem that

test(\%HoA1, \%HoA2, @a, "test");
is the same thing as
test(\%HoA1, \%HoA2, "entry1", "entry2", "entry3", "test");

When you do

my ($HoA1,$HoA2,@a,$test) = @_;

there no way for Perl to know how many of the remaining args go in @a, so they all go into @a.

Just like with the hashes, you'll need to pass a reference to the array, not the contents of the array.


All together, we have:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; sub test { my ($HoA1, $HoA2, $a, $test) = @_; push(@{$HoA1->{"HOA-1"}}, "Entry Two"); push(@{$HoA2->{"HOA-2"}}, "Entry Two"); print "Array: @$a\n"; print "Test: $test\n"; } my %HoA1; my %HoA2; push(@{$HoA1{"HOA-1"}}, "Entry One"); push(@{$HoA2{"HOA-2"}}, "Entry One"); my @a = qw(entry1 entry2 entry3); test(\%HoA1, \%HoA2, \@a, "test"); print Dumper(\%HoA1); print Dumper(\%HoA2);

Note that I removed the return value since it's not needed. Since you're modifying the caller's hash through the reference, the caller already sees the changes being made by the sub.

Tip: Dumper's argument is a scalar. Don't pass a hash. If you want to dump a hash or array, pass a reference to it. It'll be much easier to read.

Update: Missed the first problem originally. Added the bit addressing it.


In reply to Re: Passing and returning multiple HoA's with Array to sub by ikegami
in thread Passing and returning multiple HoA's with Array to sub by ewhitt

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.