in reply to Changing an array from a sub
Your implementation of "sub ChangeArray6" updates the contents of @_ which were previously aliased to the contents of @A.
Note: Only the contents of @_ were changed. The contents of @A were not.
Here are two alternative ways to update the contents of @A:
In the sample above, the change made to @_ is RETURNED, then ASSIGNED to @a, changing @a.perl -E '@a=(2)x2; @a=ch(@a);sub ch{@_=(6) x @_}; say for @a'
In the second piece of code, we pass a REFERENCE to @a. The subroutine assigns new content to the thing(array ref-de-referenced) that it's first parameter referenced (@a).perl -E '@a=(2)x2; ch(\@a);sub ch{@{$_[0]}=(6) x scalar @{$_[0]}}; say + for @a'
See 'perldoc perlref'.
Python is a racist language what with it's dependence on white space!
|
|---|