Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: How to pass two lists to a sub?

by tphyahoo (Vicar)
on Oct 18, 2006 at 10:39 UTC ( #579012=note: print w/ replies, xml ) Need Help??


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

use strict; use warnings; use Data::Dumper; my @list1 = qw(a b c); my @list2 = qw(d e f); printem([@list1], [@list2]); sub printem { my $list1 = shift or die "no list1"; my $list2 = shift or die "no list2"; print "list1: " . Dumper($list1); print "list2: " . Dumper($list2); }


Comment on Re: How to pass two lists to a sub?
Download Code
Re^2: How to pass two lists to a sub?
by wazoox (Prior) on Oct 18, 2006 at 11:26 UTC
    Please notice that
    printem([@list1], [@list2]);
    actually creates two anonymous arrays and copy data in memory, while
    printem(\@list1, \@list2)
    will pass references to the original arrays.
    Copying data will prevent accidental changes of the original arrays but may be quite long and memory-hungry if the copied data is big...
    Here's a small sample to picture the difference :
    #!/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);
      Hi
      Thanks a lot for your suggestion.
      Regd's
      Sanjay

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://579012]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (10)
As of 2014-09-16 21:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (48 votes), past polls