in reply to Rotating an array

I guess you would have to benchmark them to see which is better.
#!/usr/bin/perl use warnings; use strict; #4.47: How do I handle circular lists? # Circular lists could be handled in the traditional fashion with l +inked # lists, or you could just do something like this with an array: # unshift(@array, pop(@array)); # the last shall be first # push(@array, shift(@array)); # and vice versa # You can also use "Tie::Cycle": use Tie::Cycle; tie my $cycle, 'Tie::Cycle', [ qw( FFFFFF 000000 FFFF00 ) ]; print $cycle; # FFFFFF print $cycle; # 000000 print $cycle; # FFFF00 print $cycle; # FFFFFF print $cycle; # 000000 print $cycle; # FFFF00

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

Replies are listed 'Best First'.
Re^2: Rotating an array
by rovf (Priest) on Jun 17, 2011 at 12:24 UTC
    IMO Tie::Cycle has two disadvantages: It always rotates the list by only one position (hence is not better than the push(...,pop...) variant), and it rearranges the list every time I access the tie'd variable. No surprise: Tie::Cycle was made for a completely different purpose than I'm going to use it: I want to rotate the array only once, but then by a certain number of positions.

    However, the solution using splice, which moritz proposed, looks better than those I came up with. I thiink I will use it.

    -- 
    Ronald Fischer <ynnor@mm.st>