I am doing a query from a database and returning two pieces of data. A name, and a status code. I am attempting to use the name (First and Last) as a key and count the number of instances it appears in my data set. For some reason it will count the first instance, but will not give me a count above one.
Here is the code.
Here is the code.
Code:
#!/usr/local/bin/perl
use warnings;
use strict;
use DBI;
my @data = (); # array of numbers
my %CB = (); # has of number frequencies
my %LM = (); # has of number frequencies
my %RF = (); # has of number frequencies
my $dbh = DBI->connect('task','','','AtomixDBD');
my $sql =
"select tsrmaster.sname, call_history.status ".
"from tsrmaster, call_history ".
"WHERE tsrmaster.tsr = call_history.tsr ".
"and call_history.status = \"LM\" ".
"and time_connect < 20 ".
"UNION ".
"select tsrmaster.sname, call_history.status ".
"from tsrmaster, call_history ".
"WHERE tsrmaster.tsr = call_history.tsr ".
"and call_history.status = \"CB\" ".
"and time_connect < 180 ".
"UNION ".
"select tsrmaster.sname, call_history.status ".
"from tsrmaster, call_history ".
"WHERE tsrmaster.tsr = call_history.tsr ".
"and call_history.status = \"R\" ".
"and time_connect > 120 ";
my $psql = $dbh->prepare($sql);
my $rc = $psql->execute;
while (@data = $psql->fetchrow) {
my $name = $data[0];
my $dispo = $data[1];
$name =~ s/^\s+//;
$name =~ s/\s+$//;
if ($dispo eq "CB") {
$CB{$name}++;
}
if ($dispo eq "LM") {
$LM{$name}++;
}
if ($dispo eq "R ") {
$RF{$name}++;
}
}
$psql->finish;
$dbh->disconnect;
foreach my $key (sort keys %CB) {
print "Key: $key Value: $CB{$key} \n";
}
foreach my $key (sort keys %LM) {
print "Key: $key Value: $RF{$key} \n";
}
foreach my $key (sort keys %RF) {
print "Key: $key Value: $RF{$key} \n";
}