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; }
In reply to Preserve the value of original array by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |