Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
In the code below, I pass the array @num as a reference to the subroutine second. At second, the array is changed and some value (not directly related to the array) is returned to the calling sub. I want to preserve the value of the original array @num after the operation at second. What is the best way to do that?
Thank you so much.use strict; my @num = qw(1 2 3 4 5); first(); sub first { print "\@num before: @num\n"; # prints 1 2 3 4 5 my $result = second(\@num); print "result: $result\n"; print "\@num after: @num\n"; # prints 1 2 3 4 5 6 # But I would need it to still print # 1 2 3 4 5 } sub second { my $num_ref = shift; push(@$num_ref, 6); my $sum = 0; foreach (@$num_ref) { $sum += $_; } return $sum; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Preserve the value of original array
by bobf (Monsignor) on Feb 14, 2006 at 06:09 UTC | |
by Anonymous Monk on Feb 14, 2006 at 06:28 UTC | |
by ayrnieu (Beadle) on Feb 14, 2006 at 07:01 UTC | |
|
Re: Preserve the value of original array
by McDarren (Abbot) on Feb 14, 2006 at 06:07 UTC | |
by Anonymous Monk on Feb 14, 2006 at 06:16 UTC | |
by McDarren (Abbot) on Feb 14, 2006 at 06:20 UTC | |
|
Re: Preserve the value of original array
by Anonymous Monk on Feb 14, 2006 at 06:12 UTC |