Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Dear monks,
I need to iterate over a database table, and I need to process them in groups based on a key ("foo_id" in the example). What would be a good perlish way of getting them into an array for processing? Does DBI provide such a function?
This is what I came up with:
my $sth = $dbh->prepare( "SELECT foo_id, col1, col2 FROM a ORDER BY foo_id"); $sth->execute(); my @group; while (my $row = $sth->fetchrow_hashref) { if (!@group or $row->{foo_id} == $group[0]->{foo_id}) { push @group, $row; } else { # print Dumper(\@group); process(\@group); @group = ($row); } }
but I'm afraid it doesn't process the last group.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: DBI & processing groups of rows
by wind (Priest) on Apr 09, 2011 at 21:34 UTC | |
by Anonymous Monk on Apr 10, 2011 at 07:42 UTC | |
by wind (Priest) on Apr 11, 2011 at 05:02 UTC | |
by Anonymous Monk on Apr 15, 2011 at 06:05 UTC |