Re: swap by reference?



..rhavin grobert wrote:
> 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'....

Waaay too much trouble you just went through.

$a = 'A';
$b = 'B';
($a, $b) = ($b, $a);
print "\$a: $a, \$b: $b\n";


> now i want to do something similar with arrays - that i have to pass by
> reference, eg:
>
> swap \@a,\@b;

$a = [1..5];
$b = ['a'..'e'];
($a, $b) = ($b, $a);
print "A: @$a, B: @$b\n";'


>
> ...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?

You're not passing a mutable reference into the subroutine. You can't
change the reference that's created by \@a any more than you can change
the string "foobar". You have to store the reference in a variable and
change that variable.

@a = (1..5);
@b = ('a'..'e');
my ($a_ref, $b_ref) = (\@a, \@b);
swap ($a_ref, $b_ref);
sub swap {
my ($ref1, $ref2) = @_;
($_[0], $_[1]) = ($ref2, $ref1);
}
print "A: @$a_ref, B: @$b_ref\n";


But again, there's really no need for the subroutine. Just swap them
in a single list assignment.

Paul Lalli

.



Relevant Pages

  • 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 ... dont know how to swap the references. ... should undef array a, ...
    (perl.beginners)
  • Re: Using ref
    ... If the parameter were passed by reference by default, ... To demonstrate that for most stuff, except for swap, 'new' and stuff ... As you can see for the string passing by reference ... public void FooClassSB1 ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: qsort and arbitrary types
    ... >> or swap elements of the type pointed to. ... > an array of arbitrary type, ... > a reference to a comparison function. ...
    (comp.lang.fortran)
  • Re: How to update an agrument passed by name in scheme
    ... but it passes compound values by reference. ... Those two values are passed to the swap! ... to affect variables in its caller. ...
    (comp.lang.scheme)
  • Re: How to update an agrument passed by name in scheme
    ... but it passes compound values by reference. ... (define (swap! ... No. "Scheme passes arguments by value" means that in the expression ... formal semantics of Scheme are presented. ...
    (comp.lang.scheme)