in reply to Counting fields from database
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Counting fields from database (updated)
by htmanning (Friar) on Oct 03, 2020 at 00:49 UTC | |
|
Re^2: Counting fields from database (updated)
by htmanning (Friar) on Oct 03, 2020 at 01:31 UTC | |
by haukex (Archbishop) on Oct 03, 2020 at 07:29 UTC | |
by htmanning (Friar) on Oct 03, 2020 at 21:34 UTC |