On Mon, Jul 01, 2002 at 12:23:46PM +0000, sara starre wrote:
> >sub rotate { unshift @_, splice @_, shift @_; return @_ }
>
> Curious. Here is one of my routines that rotates a vector (an array) either
> foward or reverse, any number of elements. Seems like we had a similar
> approach I just encapuslated with a bunch of logic and control..
>
>
>
> # rotate elements around, arg is n and array
> sub rotate
> {return () unless $_[1];
> die 'APL: rotate called with improper args' unless $_[0] && $_[1];
> my $n=$_[0];
> my @l=@{$_[1]};
> my ($i, @lx) = (0);
>
> for ($i==0; $i<abs $n; $i++)
> {$lx=shift @l if $n>0;
> $lx=pop @l if $n<0;
> push @l,$lx if $n>0;
> unshift @l,$lx if $n<0;
> }
>
> return \@l;
> }
Eeew. That's so horribly inefficient and unPerllike.
Try this:
sub rotate {
return () unless $_ [1];
return [] unless @{$_ [1]};
$_ [0] %= @{$_ [1]};
[@{$_ [1]} [$_ [0] .. $#{$_ [1]}, 0 .. $_ [0] - 1]]
}
Abigail