prints: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); }
If you are not concerned about protecting your data (usually you should be), then something like this might also work for you:list1: $VAR1 = [ 'a', 'Camel', 'c' ]; list2: $VAR1 = [ 'd', 'LLama', 'f' ]; === After changem === a, b, c d, e, f
which prints: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__
Here is more, to emphasize the difference between the two:keats, byron, frost marlowe, shakespeare, jonson
which printsuse 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); }
hope this helps.list1: $VAR1 = [ 'a', 'Camel', 'c' ]; list2: $VAR1 = [ 'd', 'LLama', 'f' ]; === After changem === a, Camel, c d, LLama, f
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |