Professional Web Applications Themes

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. #1

    Default 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. #2

    Default Re: array rotate question



    Ing. Branislav Gerzo wrote: 

    Hello,
     


    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(\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. #3

    Default 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. #4

    Default 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 );

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

  5. #5

    Default Re: array rotate question

    > how about for starters: 


    Doh! Of course there was a shorter xier way!

    Jenda and Gunnar, you always rock! ;p

    Lee.M - JupiterHost.Net
    JupiterHost.Net Guest

  6. #6

    Default 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...

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

  7. #7

    Default 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. #8

    Default Re: array rotate question

    Jenda Krynicky [JK], on Wednesday, September 22, 2004 at 00:02 (+0200)
    made these points:
     [/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 :)

    Thanks for all answers!

    --

    ...m8s, cu l8r, Brano.

    [Unknown Error on Unknown Device for Unexplainable Reason]



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

    Ing. Guest

Similar Threads

  1. Rotate Tool Won't Rotate
    By ThomZ webforumsuser@macromedia.com in forum Macromedia Freehand
    Replies: 7
    Last Post: May 7th, 04:05 PM
  2. CCW Rotate and 180 rotate in PS CS are not working properly
    By Janet_Gerhardt@adobeforums.com in forum Adobe Photoshop Mac CS, CS2 & CS3
    Replies: 3
    Last Post: April 22nd, 03:24 PM
  3. Array question
    By Chris in forum PERL Beginners
    Replies: 4
    Last Post: February 29th, 01:46 AM
  4. 4GL array question
    By Dirk in forum Informix
    Replies: 31
    Last Post: November 28th, 11:57 AM
  5. array question (grep -v on array)
    By NetComrade in forum PERL Miscellaneous
    Replies: 1
    Last Post: July 28th, 08:02 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139