Ing. Branislav Gerzo wrote:
Hello!

Hello,

I am thinking about making clear and short script to rotate array,
let's say:

input:
@list = (1 .. 20);
$start = 10;         #starting position
$values = 10;        #how much values in result


how about for starters:

my @list = qw(a b c d e f g h i j k l m n o p q r s t u v w x y z);
my $result= getsection([EMAIL PROTECTED],20,10);
for( @{ $result }) { print "-$_-\n"; }

sub getsection {
my @newa = ();
my ($aref,$strt,$amnt) = @_;
return undef if ref($aref) ne 'ARRAY' || $strt !~ m/^\d+$/ || $amnt !~ m/^\d+$/;


  my $last = scalar(@{ $aref });
  $last--;
  my $begn = 0;
  for( $strt .. (($strt + $amnt) - 1) ) {
     if($_ <= $last) { push @newa, $aref->[$_]; }
     else { push @newa, $aref->[$begn];$begn++; }
  }
  return [EMAIL PROTECTED];
}


how to get output: @result = ( 5, 6, 7, 8, 9, 11, 12, 13, 14, 15 ); #10 values (I don't want $start in @result)

ofcoure script should work with overlapping too:
@list = (1 .. 20);
$start = 18;
$items = 10;

output:
@result = ( 13, 14, 15, 16, 17, 19, 20, 1, 2, 3 ); #10 values

any ideas ?

Why is there a difference of 5 each time:

$start = 10
$result[0] = 5

$start = 18
$result[0] = 13

That's throwing me a bit

HTH :)

Lee.M - JupiterHost.Net

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to