Re: array rotate question
Jenda Krynicky [JK], on Wednesday, September 22, 2004 at 00:02 (+0200) made these points: $values = 10;#how much values in result 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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: array rotate question
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
Re: array rotate question
From: Ing. Branislav Gerzo [EMAIL PROTECTED] 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 many (taky si to furt pletu) 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 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 = [EMAIL PROTECTED] === 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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: array rotate question
Ing. Branislav Gerzo wrote: 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) Did you suddenly change $start to 4? 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 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( [EMAIL PROTECTED], $start, $values ); -- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: array rotate question
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]; } Doh! Of course there was a shorter sexxier way! Jenda and Gunnar, you always rock! ;p 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
Re: array rotate question
JupiterHost.Net wrote: Doh! Of course there was a shorter sexxier way! Jenda and Gunnar, you always rock! ;p 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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: array rotate question
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 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 ? my @result = @list[ map { $_ @list ? $_ : $_ - $#list - 1 } $start .. $start + $items - 1 ]; John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response