English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I've looked everywhere , and I haven't got a clear answer.
I want to pass an array to a function which will modify it . How do I do that?

2007-07-04 20:59:52 · 4 answers · asked by Yossale 1 in Computers & Internet Programming & Design

4 answers

First, let's define a function that expects a reference to an array. When the array is large, it's much faster to pass it in as a single reference than a long list of values:

$total = sum ( \@a );

sub sum {
my ($aref) = @_;
my ($total) = 0;
foreach (@$aref) { $total += $_ }
return $total;
}

Let's pass in several arrays to a function and have it pop each of them, returning a new list of all their former last elements:

@tailings = popmany ( \@a, \@b, \@c, \@d );

sub popmany {
my @retlist = ();
for my $aref (@_) {
push @retlist, pop @$aref;
}
return @retlist;
}

Here's how you might write a function that does a kind of set intersection by returning a list of keys occurring in all the hashes passed to it:

@common = inter( \%foo, \%bar, \%joe );
sub inter {
my %seen;
for my $href (@_) {
while (my $k = each %$href ) {
$seen{$k}++;
}
}
return grep { $seen{$_} == @_ } keys %seen;
}

So far, we're just using the normal list return mechanism. What happens if you want to pass or return a hash? Well, if you're only using one of them, or you don't mind them concatenating, then the normal calling convention is okay, although a little expensive.

As explained earlier, where people get into trouble is here:

(@a, @b) = func(@c, @d);
or here:
(%a, %b) = func(%c, %d);
That syntax simply won't work. It just sets @a or %a and clears @b or %b. Plus the function doesn't get two separate arrays or hashes as arguments: it gets one long list in @_, as always.

You may want to arrange for your functions to use references for both input and output. Here's a function that takes two array references as arguments and returns the two array references ordered by the number of elements they have in them:

($aref, $bref) = func(\@c, \@d);
print "@$aref has more than @$bref\n";
sub func {
my ($cref, $dref) = @_;
if (@$cref > @$dref) {
return ($cref, $dref);
} else {
return ($dref, $cref);
}
}

2007-07-04 22:37:56 · answer #1 · answered by prasy 3 · 1 0

Perl Pass Array To Sub

2016-11-12 04:21:30 · answer #2 · answered by Anonymous · 0 0

may well be this occasion can help you. # a subroutine that sums the climate in an array # and returns the sum sub sum_array { my(@vals) = @_; # placed parameters in array @vals my($sum) = 0; # initialize the sum to 0 # $sum is inner maximum so we don't could desire to fret approximately # clobbering any international variable named $sum! foreach $i (@vals) { $sum = $sum + $i; } return($sum); } # substantial software print "enter a gaggle of numbers on a linen"; $_ = ; @nums = split; print "The sum is ", &sum_array(@nums), "n"; # simplary u can create subroutine for multiply. sub multi_array { my(@vals) = @_; # placed parameters in array @vals my($multi) = a million; # initialize the multi to a million # $multi is inner maximum so we don't could desire to fret approximately # clobbering any international variable named $multi foreach $i (@vals) { $multi= $multi * $i; } return($multi); } Regards

2016-11-08 04:57:14 · answer #3 · answered by heyder 4 · 0 0

I think this is the link you are looking for.

2007-07-04 21:12:00 · answer #4 · answered by AnalProgrammer 7 · 0 0

fedest.com, questions and answers