The code above protects your original data against changes in the called subroutine by allocating new anonymous arrays, filling them with the data from the original arrays, and only passing references to the new anonymous arrays to the subroutine. In general, that's a good thing.
This code, for example:
use strict; use warnings; use Data::Dumper; my @list1 = qw(a b c); my @list2 = qw(d e f); changem([@list1], [@list2]); print "\n=== After changem ===\n"; printf "%s\n", join q(, ), @list1; printf "%s\n", join q(, ), @list2; sub changem { my $aref1 = shift or die "no list1"; my $aref2 = shift or die "no list2"; $aref1->[1] = 'Camel'; $aref2->[1] = 'LLama'; print "list1: " . Dumper($aref1); print "list2: " . Dumper($aref2); }
prints:
list1: $VAR1 = [ 'a', 'Camel', 'c' ]; list2: $VAR1 = [ 'd', 'LLama', 'f' ]; === After changem === a, b, c d, e, f
If you are not concerned about protecting your data (usually you should be), then something like this might also work for you:
use strict; use warnings; my @a1 = qw(keats byron frost); my @a2 = qw(marlowe shakespeare jonson); handle_two( \@a1, \@a2 ); exit(0); sub handle_two { my ($aref1, $aref2) = @_; for my $aref ($aref1, $aref2) { do_something_with_one( $aref ); } return; } sub do_something_with_one { my ($aref) = @_; printf "%s\n", join q(, ), @$aref; return; } __END__
which prints:
keats, byron, frost marlowe, shakespeare, jonson
Here is more, to emphasize the difference between the two:
use strict; use warnings; use Data::Dumper; my @list1 = qw(a b c); my @list2 = qw(d e f); changem(\@list1, \@list2); print "\n=== After changem ===\n"; printf "%s\n", join q(, ), @list1; printf "%s\n", join q(, ), @list2; sub changem { my $aref1 = shift or die "no list1"; my $aref2 = shift or die "no list2"; $aref1->[1] = 'Camel'; $aref2->[1] = 'LLama'; print "list1: " . Dumper($aref1); print "list2: " . Dumper($aref2); }
which prints
list1: $VAR1 = [ 'a', 'Camel', 'c' ]; list2: $VAR1 = [ 'd', 'LLama', 'f' ]; === After changem === a, Camel, c d, LLama, f
hope this helps.

In reply to Re: How to pass two arrays to a sub? by fenLisesi
in thread How to pass two arrays to a sub? by sanjay nayak

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.