I need to know the total number of threadIDs

It's a little unclear to me from your description whether you just need to know this information, in which case you can ask the database to provide it with a GROUP BY clause, or whether you're looping through all messages anyway, and you just want to count thread IDs*. In the latter case, since I assume the thread IDs are a unique identifier per thread, you can do this with a hash. Here are examples of both:

use warnings; use strict; use Data::Dump; use DBI; # 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; # ask the DB to give us per-thread information dd $dbh->selectall_arrayref( 'SELECT threadID, COUNT(*) FROM messages GROUP BY threadID'); # => [ [0, 16], [1, 16], [2, 16], [3, 16], # [4, 16], [5, 16], [6, 16], [7, 16], ] # loop through rows and count threads ourselves my $sth_s = $dbh->prepare( 'SELECT msgID, threadID from messages'); $sth_s->execute; my %threadIDs; while ( my $row = $sth_s->fetchrow_hashref ) { $threadIDs{ $row->{threadID} }++; } dd \%threadIDs; # => { "0" => 16, "1" => 16, "2" => 16, "3" => 16, # "4" => 16, "5" => 16, "6" => 16, "7" => 16 }

* Update: Upon rereading, it's probably the latter. By the way, $current_count_all <= $stopcount_all sounds like you want to limit the number of records returned, which you can also do in the database, e.g. in MySQL with a LIMIT clause.

Update 2: For completeness, there are two issues with the approach you showed in the OP. First, you need to be certain that $threadID doesn't ever contain the separator character ("," in this case), and your regex is too simple in that it will also match partial IDs (e.g. /234/ will match in "1234,5678"), which you'd need to prevent by anchoring the regex appropriately - for example, if the IDs are integers, you could say /\b\Q$threadID\E\b/) (note I've used \Q...\E to escape any special characters in $threadID even though I just said they're integers; it's just to play it extra safe). But the method of searching the string for the $threadID will be way less efficient than a hash, so I would always recommend that instead. Also modified the first example to supply the threadID in addition to the count.


In reply to Re: Counting fields from database (updated) 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.