in reply to Re: How to pass two lists to a sub?
in thread How to pass two arrays to a sub?
actually creates two anonymous arrays and copy data in memory, whileprintem([@list1], [@list2]);
will pass references to the original arrays.printem(\@list1, \@list2)
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @list1 = qw(a b c); my @list2 = qw(d e f); sub sem { my $list1 = shift or die "no list1"; my $list2 = shift or die "no list2"; $list1->[0]='x'; @{$list2}=qw(D E F); } # print original data print "list1: " . Dumper(@list1); print "list2: " . Dumper(@list2); # call with copy sem([@list1], [@list2]); print "list1: " . Dumper(@list1); print "list2: " . Dumper(@list2); # call by reference sem(\@list1, \@list2); print "list1: " . Dumper(@list1); print "list2: " . Dumper(@list2);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: How to pass two lists to a sub?
by sanjay nayak (Sexton) on Oct 18, 2006 at 13:36 UTC |