in reply to Preserve the value of original array

I want to preserve the value of the original array @num after the operation at second. What is the best way to do that?
Just take a copy of it in your second sub and work on that. For example:
sub second { my $num_ref = shift; my @copy_of = @$num_ref; push(@copy_of, 6); my $sum = 0; foreach (@copy_of) { $sum += $_; } return $sum; }
Cheers,
Darren :)

Replies are listed 'Best First'.
Re^2: Preserve the value of original array
by Anonymous Monk on Feb 14, 2006 at 06:16 UTC
    Thanks, Darren :)

    I need to copy the array into an array, and not make a copy of the reference, as in:

    sub second { my $num_ref = shift; my $copy_of_ref = $num_ref; push(@$copy_of_ref, 6); my $sum = 0; foreach (@$copy_of_ref) { $sum += $_; } return $sum; }
    Am I right?
      Yes, that's fine. But please also take note of bobf's comments below :)

      Update:Actually, I misread what you said there. Either way is okay. You can either take a copy of the reference, or dereference it into another array. But again, as bobf already pointed out in his update - the simplest way is to just pass the array (rather than a reference) - and then take a copy of that.