Sorry, but your code is not runnable, and doesn't seem to be representative of the problem you're having. Please see How do I post a question effectively?, I know what I mean. Why don't you?, and Short, Self-Contained, Correct Example.

This is a guess of what you want that I've pieced together from your various posts.

use warnings; use strict; use Data::Dump; use DBI; my $titlesperpage = 10; # set up a dummy database my $dbh = DBI->connect("dbi:SQLite::memory:", undef, undef, { RaiseError=>1 }); $dbh->do(<<'END'); CREATE TABLE messages ( msgID INTEGER, threadID INTEGER ) END my $sth_i = $dbh->prepare( 'INSERT INTO messages (msgID, threadID) VALUES (?,?)'); $sth_i->execute($_,$_>>4) for 0..127; for my $currentpage (1..5) { # simulate requests for different pages my $startcount = ($currentpage - 1) * $titlesperpage + 1; my $stopcount = $currentpage * $titlesperpage; dd $startcount, $stopcount; my $sth_s = $dbh->prepare('SELECT msgID, threadID FROM messages'); $sth_s->execute; my %threadIDs; my $rowcnt = 1; while ( my $row = $sth_s->fetchrow_hashref ) { $threadIDs{ $row->{threadID} }++; if ( $rowcnt >= $startcount && $rowcnt <= $stopcount ) { dd $row->{msgID}; } } continue { $rowcnt++ } dd \%threadIDs; }

But this loops through all results for every request. You can have the database do the pagination, even though you have to hit the database twice it still doesn't require you to loop through all records, only those for the current page.

for my $currentpage (1..5) { # simulate requests for different pages my $startcount = ($currentpage - 1) * $titlesperpage + 1; dd $dbh->selectall_arrayref( 'SELECT threadID, COUNT(*) FROM messages GROUP BY threadID'); my $sth_s = $dbh->prepare('SELECT msgID, threadID FROM messages ' .'ORDER BY msgID LIMIT ?,?'); $sth_s->execute($startcount, $titlesperpage); while ( my $row = $sth_s->fetchrow_hashref ) { dd $row->{msgID}; } }

In reply to Re^3: Counting fields from database by haukex
in thread Counting fields from database by htmanning

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.