in reply to interchanging variables the tough way
The canonical answer (that perl is able to sometimes avoid) is to use a temporary variable, like so:
my $temp; $temp=$a; $a=$b; $b=$c; $c=$temp;
In the above code, we save the value of $a to a temporary variable. Then we assign $a to $b - without worrying about clobbering the value of $a, because we have just saved it. And now that the value in $b has been copied to $a, we can safely assign into $b, etc. etc...
Update: Thanks to those below. Yes, it should assign $c back to $temp, not $a.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE (tilly) 2: interchanging variables the tough way
by tilly (Archbishop) on Aug 30, 2000 at 18:40 UTC | |
|
RE: Re: interchanging variables the tough way
by ColtsFoot (Chaplain) on Aug 30, 2000 at 18:40 UTC | |
|
RE: Re: interchanging variables the tough way
by ANKUR (Novice) on Aug 30, 2000 at 18:53 UTC |