in reply to reordering lists

just because row 27,895 out of 500,000 was repositioned to slot 3, now 499,998 rows will have their id recalculated.
Not so. Only the ones between the old and new positions need to be recalculated.
sub move { my ($from, $to) = @_; my $temp = $row[$from]; if ($from < $to) { for ($from+1..$to) { $row[$_-1] = $row[$_]; } } else { for (0..$from-$to-1) { $row[$from-$_] = $row[$from-$_-1]; } } $row[$to] = $temp; }

The PerlMonk tr/// Advocate

Replies are listed 'Best First'.
Re: Re: reordering lists
by punkish (Priest) on Jan 20, 2004 at 21:44 UTC
    yes, you are right. My bad. Of course, in the worst case (if the last slot is repositioned to the first slot) all the rows will have to be recalculated.

    What you are suggesting is what I had resigned myself to doing with the database. My guess is, DBI should be fast enough to not worry about it.

    Its just that it seems like an inelegant method. Hence, my question in the first place.