swap by reference?



i understand that i can swap scalars by reference, eg:

sub swap {my $tmp=$_[0]; $_[0]=$_[1]; $_[1]=$tmp;}

....so when i say

$a = 'A';
$b = 'B';
swap $a, $b;

then a 'print $a;' gives me a 'B'....


now i want to do something similar with arrays - that i have to pass by
reference, eg:

swap \@a,\@b;

....but even if i can acces the arrays via '$$...' inside the sub, i
dont know how to swap the references. i know i could process the arrays
field by field but that isn't what i want.
What i want is to make the memory-adress behind the statements '@a' and
'@b' swap.
as far as i understand this, passing a reference (lets say $x=\$y) to a
sub and then (for example) undefining it ($x=undef;) doesn't undef $y.
doing the same with $$x=undef does undef $y.

so when i say
sub magic { $$_[0] = undef;}

then a magic(\@a); should undef array a, but it doesnt.

can anyone please explain this and tell me, what i'm doing wrong?

.



Relevant Pages

  • Re: swap by reference?
    ... > i understand that i can swap scalars by reference, ... > now i want to do something similar with arrays - that i have to pass by ... You're not passing a mutable reference into the subroutine. ...
    (perl.beginners)
  • Re: call by reference
    ... all parameters are passed by reference and at least if we ignore how ... arrays fit into the picture. ... is illegal to have the operand be anything except a variable. ... The situation gets a bit more interesting once you have aggregate ...
    (comp.lang.scheme)
  • Re: subroutine definitions
    ... > the reference directly in the parameter list. ... I seldom use statically declared arrays or hashes myself, unless they are scope to a ... written a lot of scripts that generate HTML reports from the data in the MySQL DB. ...
    (perl.beginners)
  • Re: Array comparison
    ... > Deep and recursive. ... the pointers and comparing their referents instead of comparing the ... >> arrays, then I think you also need to argue against assigning arrays. ... semantics (notably, reference counting). ...
    (alt.comp.lang.borland-delphi)
  • Re: A VB.NET Critique
    ... the declarations like so: ... in .NET are actually *reference* types its just that *some* of those ... from a language design point of view and is of more relevance to those ... > (such as arrays and strings) just eludes me somewhat; ...
    (comp.programming)