in reply to How to pass two arrays to a sub?

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.

Replies are listed 'Best First'.
Re^2: How to pass two arrays to a sub?
by Hue-Bond (Priest) on Oct 18, 2006 at 11:34 UTC
    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.

    But it doubles the memory requirement for the arrays so it may not be an option if the arrays are large.

    --
    David Serrano

      True. I was trying to be as similar as possible to the original code to make it easier to understand.
      From the supplied question I can't really tell if the local copy is needed or not, so I played safe.