![]() |
Select "n" elements from an array
This would not be difficult to code, but I suspect there's a good
module out there. However, my CPAN search skills have proven inadequate to locate it. I want to iterate over an array, selecting "n" elements at a time (where "n" is an arbitrary number), something like: foreach my @three_elements ( ?something?(1..10, 3) ) { # @three_elements has three numbers in it } As the loop iterates, @three_elements would be: (1,2,3) (4,5,6) (7,8,9) (10,undef,undef) It would not be difficult to code, but I have grasped the concept of code reuse. Can someone kindly direct me to a module which does something like this? I really think that such a module must exist - it seems a common enough task. -- http://DavidFilmer.com |
Re: Select "n" elements from an array
use...@DavidFilmer.com wrote: > This would not be difficult to code, but I suspect there's a good > module out there. However, my CPAN search skills have proven inadequate > to locate it. > > I want to iterate over an array, selecting "n" elements at a time > (where "n" is an arbitrary number), something like: > > foreach my @three_elements ( ?something?(1..10, 3) ) { > # @three_elements has three numbers in it > } > > As the loop iterates, @three_elements would be: > (1,2,3) > (4,5,6) > (7,8,9) > (10,undef,undef) Well, a module can't change the behaviour of for() or .. so it would be more like foreach my @$three_elements ( something(3 ,1..10) ) { # @$three_elements has three numbers in it } > It would not be difficult to code, but I have grasped the concept of > code reuse. Can someone kindly direct me to a module which does > something like this? I really think that such a module must exist - it > seems a common enough task. It is a common task but usually it's OK to do it destructively. And if it's not you can always make a copy. my @stuff = (1..10); while ( my @three_elements = splice @stuff, 0, 3 ) { Note on the last interation @three_elements will have fewer elements but this probably doesn't matter as $three_elements[1] will still be undef. |
Re: Select "n" elements from an array
usenet@DavidFilmer.com wrote:
> This would not be difficult to code, but I suspect there's a good > module out there. However, my CPAN search skills have proven inadequate > to locate it. > > I want to iterate over an array, selecting "n" elements at a time Either List::Util or List::MoreUtils has just such a function, named (not surprisingly) "natatime" Paul Lalli |
Re: Select "n" elements from an array
Brian McCauley wrote:
> Well, a module can't change the behaviour of for() or .. Really? I was under the impression that a module that implements Source Filtering can pretty much change any language construct... Paul Lalli |
Re: Select "n" elements from an array
Paul Lalli wrote:
> Either List::Util or List::MoreUtils has just such a function, named > (not surprisingly) "natatime" It is List::MoreUtils - Thanks, Paul - that's EXACTLY what I was looking for (and I should have guessed that Tassilo would have included it in this Swiss Army Knife of list processing). -- http://DavidFilmer.com |
Re: Select "n" elements from an array
Brian McCauley <nobull67@gmail.com> wrote in comp.lang.perl.misc:
> > use...@DavidFilmer.com wrote: > > This would not be difficult to code, but I suspect there's a good > > module out there. However, my CPAN search skills have proven inadequate > > to locate it. > > > > I want to iterate over an array, selecting "n" elements at a time > > (where "n" is an arbitrary number), something like: > > > > foreach my @three_elements ( ?something?(1..10, 3) ) { > > # @three_elements has three numbers in it > > } > > > > As the loop iterates, @three_elements would be: > > (1,2,3) > > (4,5,6) > > (7,8,9) > > (10,undef,undef) [...] > It is a common task but usually it's OK to do it destructively. And if > it's not you can always make a copy. > > my @stuff = (1..10); > while ( my @three_elements = splice @stuff, 0, 3 ) { > > Note on the last interation @three_elements will have fewer elements > but this probably doesn't matter as $three_elements[1] will still be > undef. List::MoreUtils::natatime has been pointed out, so let's have some fun. Non-destructive, and to specification with the final undef's: for ( grep $_ % 3 == 0, 0 .. $#stuff ) { my @three_elements = @stuff[ $_ .. $_ + 2]; # ... } Disadvantage: The parameter (3) appears twice, though it looks like 2 in one case. Anno -- If you want to post a followup via groups.google.com, don't use the broken "Reply" link at the bottom of the article. Click on "show options" at the top of the article, then click on the "Reply" at the bottom of the article headers. |
Re: Select "n" elements from an array
usenet@DavidFilmer.com wrote:
> Paul Lalli wrote: > > > Either List::Util or List::MoreUtils has just such a function, named > > (not surprisingly) "natatime" > > It is List::MoreUtils - Thanks, Paul - that's EXACTLY what I was > looking for (and I should have guessed that Tassilo would have included > it in this Swiss Army Knife of list processing). > I'd be remiss if I didn't mention, ahem, *my* module: Array::Each. use strict; use warnings; use Array::Each; my @arr = ( 1 .. 10 ); my $are = Array::Each->new( \@arr ); $are->set_group( 3 ); $are->set_undef( 'undef' ); while( my ( $x, $y, $z ) = $are->each() ) { print "( $x, $y, $z )\n"; } __END__ ( 1, 2, 3 ) ( 4, 5, 6 ) ( 7, 8, 9 ) ( 10, undef, undef ) I have not benchmarked this against List::MoreUtils, but I suspect natatime will blow it out of the water. :-) When I looked at the source for List::MoreUtils::each_array, I said, "Dang, I wish I'd done it that way." Perl6 will have each and zip, which will make some of this moot anyway. Cheers, -- Brad |
| All times are GMT. The time now is 06:51 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.