Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Select "n" elements from an array

Reply
Thread Tools

Select "n" elements from an array

 
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      04-25-2006
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

 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      04-25-2006

(E-Mail Removed) 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.

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      04-25-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      04-25-2006
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

 
Reply With Quote
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      04-25-2006
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

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      04-25-2006
Brian McCauley <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>
> (E-Mail Removed) 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.
 
Reply With Quote
 
Brad Baxter
Guest
Posts: n/a
 
      04-26-2006
(E-Mail Removed) 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Modifying Select Elements in Array of Objects Glenn Ritz Ruby 3 03-20-2010 11:03 PM
XML elements to JavaScript Array elements Conversion P XML 1 07-07-2006 09:08 PM
clear an html SELECT and next insert in it all the elements of an array SAN CAZIANO Javascript 1 11-25-2004 02:40 PM
searching elements of an array within another array diffused Java 9 08-01-2004 10:09 AM
select of select box will select multiple in another box palmiere Javascript 1 02-09-2004 01:11 PM



Advertisments