Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Expected Output:#!/usr/local/bin/perl ### Temp script to replicate oid_mod.pl ### use strict; my @a1 = qw(a b c); my @a2 = qw(d e f); foreach (@a2) { print "$_\n"; } changem(\@a1,\@a2); #pass-by-ref to allow extraction foreach (@a2) { print "$_\n"; } sub changem { my ($r1,$r2) = @_; my @arr1 = @{$r1}; my @arr2 = @{$r2}; print "BEFORE: $arr2[1]\n"; $arr2[1] = "q"; print "AFTER: $arr2[1]\n"; }
Actual Output:d e f BEFORE: e AFTER: q d q f
Why, when I print @a2 after the subroutine, has the value of $a2[1] not changed?d e f BEFORE: e AFTER: q d e f
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Array references inside a subroutine
by tcf22 (Priest) on Jul 16, 2003 at 19:59 UTC | |
|
Re: Array references inside a subroutine
by thelenm (Vicar) on Jul 16, 2003 at 20:03 UTC | |
|
Re: Array references inside a subroutine
by Anonymous Monk on Jul 16, 2003 at 20:11 UTC | |
|
Re: Array references inside a subroutine
by Mago (Parson) on Jul 17, 2003 at 01:23 UTC |