Iterator::GroupedRange - Iterates retrieving a set of specified number rows
use Iterator::GroupedRange; my @ds = ( [ 1 .. 6 ], [ 7 .. 11 ], [ 11 .. 25 ], ); my $i1 = Iterator::GroupedRange->new( sub { shift @ds; }, 10 ); $i1->next; # [ 1 .. 10 ] $i1->next; # [ 11 .. 20 ] $i1->next; # [ 21 .. 25 ] my $i2 = Iterator::GroupedRange->new( [ 1 .. 25 ], 10 ); $i2->next; # [ 1 .. 10 ] $i2->next; # [ 11 .. 20 ] $i2->next; # [ 21 .. 25 ]
Iterator::GroupedRange is module to iterate retrieving a set of specified number rows. Code reference or list reference becomes provider of sets.
It accepts other iterator to get rows, or list.
Return new instance. Arguments details are:
The code reference must be taking a list reference or undef. If the return value is undef or empty array reference, #has_next() will return false value.
This list reference will be code reference that will be return a set of specified number rows.
Most number of retrieving rows by each iteration. Default value is 1000.
Grouped size.
Number of rows. For example, using DBI's statement handle:
my $sth = $dbh->prepare('SELECT blah FROM example'); $sth->execute; my $iter; $iter = Iterator::GroupedRange->new(sub { if ( my $ids = $sth->fetchrow_arrayref( undef, $iter->range ) ) { return [ map { $_->[0] } @$ids ]; } else { return; } }, { rows => $sth->rows, range => 1000 });
Return which the iterator has next rows or not.
Return next rows.
Return which the iterator becomes ended of iteration or not.
Append new items.
Return grouped size.
Return total rows.
Toru Yamaguchi <zigorou@cpan.org>
List::MoreUtils has natatime subroutine looks like this module. The natatime subroutine can treat only list.
natatime
DBI's fetchall_arrayref can accepts max_rows argument. This feature is similar to this module. For example:
use DBI; use Data::Dumper; my $sth = $dbh->prepare('SELECT id FROM people'); while ( my $ids = $sth->fetchall_arrayref(undef, 100) ) { $ids = [ map { $_->[0] } @$ids ]; warn Dumper($ids); }
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Iterator::GroupedRange, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Iterator::GroupedRange
CPAN shell
perl -MCPAN -e shell install Iterator::GroupedRange
For more information on module installation, please visit the detailed CPAN module installation guide.