To do this, pass references to the arrays and dereference in the subroutine:

process_something($scalar1, $scalar2, \@list1, \@list2); sub process_something { my ($scalar1, $scalar2, $ra_list1, $ra_list2) = @_; my @list1 = @$ra_list1; my @list2 = @$ra_list2; }

Another way is to pass all parameters in a hashref:

my $rh_params = { scalar1 => $scalar1, scalar2 => $scalar2, list1 => \@list1, list2 => \@list2, }; process_something($rh_params); sub process_something { my $rh_params = shift; my $scalar1 = $rh_params->{scalar1}; my $scalar2 = $rh_params->{scalar2}; my @list1 = @{$rh_params->{list1}}; my @list2 = @{$rh_params->{list2}}; }

The second method involves more coding but provides a number of benefits:


Update: Added missing line: process_something($rh_params);

Regards,

PN5


In reply to Re: How to pass two lists to a sub? by Prior Nacre V
in thread How to pass two lists to a sub? by YAFZ

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.