# array rotate question - PERL Beginners

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 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 ? thanks. /brano ...

1. ## array rotate question

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 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 ?
thanks.

/brano

-=x=-
Skontrolované antivírovým programom NOD32

Ing. Guest

2. ## Re: array rotate question

Ing. Branislav Gerzo wrote:

Hello,

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(\list,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 \newa;
}

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
JupiterHost.Net Guest

3. ## Re: array rotate question

From: "Ing. Branislav Gerzo" <us>

many (taky si to furt pletu)

Assuming \$values does not get bigger than the number of items in the
list and \$start is always <= the number of items in the list:

list = (list, list);
result = (
list[(\$start-\$values/2-1) .. (\$start-2)],
list[(\$start) .. (\$start+\$values/2-1)]
);

HTH, Jenda
===== cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery

Jenda Guest

4. ## Re: array rotate question

Ing. Branislav Gerzo wrote:

Did you suddenly change \$start to 4?

I fail to see how that would be the result of \$start = 18.

Anyway, even if it's unclear what you mean by start position,
something like this may be what you want:

sub rotate {
my list = { +shift };
my (\$start, \$values) = _;
unshift list, splice(list, \$start);
list[ 0 .. \$values-1 ]
}

my result = rotate( \list, \$start, \$values );

--
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Gunnar Guest

5. ## Re: array rotate question

Doh! Of course there was a shorter xier way!

Jenda and Gunnar, you always rock! ;p

Lee.M - JupiterHost.Net
JupiterHost.Net Guest

6. ## Re: array rotate question

JupiterHost.Net wrote:

Maybe Jenda, not me. Actually, both you and I misunderstood the OP.
\$start is not the beginning of a slice, it's the middle...

--
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Gunnar Guest

7. ## Re: array rotate question

Ing. Branislav Gerzo wrote:

Hello,

my result = list[ map { \$_ < list ? \$_ : \$_ - \$#list - 1 } \$start .. \$start
+ \$items - 1 ];

John
--
use Perl;
program
fulfillment
John Guest

8. ## Re: array rotate question

Jenda Krynicky [JK], on Wednesday, September 22, 2004 at 00:02 (+0200)
[/ref]
JK> many (taky si to furt pletu)

:) thanks for correcting me.

JK> Assuming \$values does not get bigger than the number of items in the
JK> list and \$start is always <= the number of items in the list:

these conditions are of course ok.

JK> list = (list, list);
JK> result = (
JK> list[(\$start-\$values/2-1) .. (\$start-2)],
JK> list[(\$start) .. (\$start+\$values/2-1)]
JK> );

very nice solution and easy to understand. Thanks a lot!
First line is the key :)

To others: sorry for my initial mail, maybe it was not clear,
\$start is in real middle position, only Jenda understood :)

--

...m8s, cu l8r, Brano.

[Unknown Error on Unknown Device for Unexplainable Reason]

-=x=-
Skontrolované antivírovým programom NOD32

Ing. Guest

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•