Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Select "n" elements from an array (http://www.velocityreviews.com/forums/t897658-select-n-elements-from-an-array.html)

usenet@DavidFilmer.com 04-25-2006 05:49 PM

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


Brian McCauley 04-25-2006 06:00 PM

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.


Paul Lalli 04-25-2006 06:11 PM

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


Paul Lalli 04-25-2006 06:13 PM

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


usenet@DavidFilmer.com 04-25-2006 06:20 PM

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


Anno Siegel 04-25-2006 06:50 PM

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.

Brad Baxter 04-26-2006 02:00 PM

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.


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