in reply to RE: interchanging variables the tough way
in thread interchanging variables the tough way

With a sub you can actually stop having to retype the list two times. Which is a nice feature:
$a=1,$b=2,$c=3,$d=4,$e=5; &rotate($a, $b, $c, $d, $e); print "\$a=$a,\$b=$b,\$c=$c,\$d=$d,\$e=$e\n"; sub rotate { my $temp = $_[0]; foreach (0..($#_ - 1)) { $_[$_] = $_[$_+1]; } $_[-1] = $temp; }
OK, this is definitely slower, but you see the point.
  • Comment on RE (tilly) 1 (possibilities): interchanging variables the tough way
  • Download Code

Replies are listed 'Best First'.
RE: (tilly) 2 (possibilities): interchanging variables the tough way
by tilly (Archbishop) on Aug 31, 2000 at 16:08 UTC
    Looking at this bugs me.

    Manipulating your input arguments this way works, but the side-effects can surprise and amaze. I would not do this lightly.

    My overall feeling is that if you feel the need to rotate your data, you should have an array rather than a list of variables. Then you could

    push @array, shift @array;
    much faster, without resorting to nasty side-effects.

    Generally when I see myself wanting to resort to "bad" techniques like side-effects, I take that as a danger sign and go looking for a more fundamental mistake in my code...