in reply to bubble sort problems

People have covered the usage of sort, but I think this is also worth mentioning -- when you're swapping two elements, it's not required to use a placeholder. Perl (at least 5.6) allows you to declare anonymous arrays, and abuse them for the greater good.

compare

$placeholder = $date[$n]; $date[$n] = $date[$n2]; $date[$n2] = $placeholder;
with
($date[$n], $date[$n2])=($date[$n2], $date[$n]);

each one swaps the contents of $date[$n] and $date[$n2], but there's (IMO) clarity in the second one, as well as a reduction in line count. Here's a small sample to flesh out the idea :
my @foo; $foo[0]=0; $foo[1]=1; print "@foo\n"; ($foo[0], $foo[1])=($foo[1], $foo[0]); print "@foo\n";

Replies are listed 'Best First'.
Re: swapping elements efficiently (boo)
by bikeNomad (Priest) on Jul 12, 2001 at 20:23 UTC
    or even terser, change:

    ($date[$n], $date[$n2])=($date[$n2], $date[$n]);
    to:
    @date[$n, $n2] = @date[$n2, $n];
      True dat.
      I was working on a script where 2 arrays got swapped around, so an array slice wasn't possible.