Re: swap by reference?
- From: "Paul Lalli" <mritty@xxxxxxxxx>
- Date: 30 Jan 2006 05:07:35 -0800
..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
.
- References:
- swap by reference?
- From: .rhavin grobert
- swap by reference?
- Prev by Date: Re: Learning Perl - the Good and the Bad (Tutorials, Habits, and Tools, etc)
- Next by Date: Re: Create thumbnail from webpage
- Previous by thread: swap by reference?
- Next by thread: Hash problem
- Index(es):
Relevant Pages
|
|