swap by reference?
- From: ".rhavin grobert" <clqrq@xxxxxxxx>
- Date: 30 Jan 2006 00:34:19 -0800
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?
.
- Follow-Ups:
- Re: swap by reference?
- From: Paul Lalli
- Re: swap by reference?
- Prev by Date: Re: Learning Perl - the Good and the Bad (Tutorials, Habits, and Tools, etc)
- Next by Date: Re: Learning Perl - the Good and the Bad (Tutorials, Habits, and Tools, etc)
- Previous by thread: how to share variable across multi-processes
- Next by thread: Re: swap by reference?
- Index(es):
Relevant Pages
|
|