Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't Count using Hash 1

Status
Not open for further replies.

V00D00

Technical User
Jul 11, 2005
78
US
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.

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";
}
 
print your data as you run the code to see what is happening.

Code:
while (@data = $psql->fetchrow) {

    my $name = $data[0];
    my $dispo = $data[1];
    $name =~ s/^\s+//;
    $name =~ s/\s+$//;
    print "\$name = $name -- \$dispo = $dispo\n";
 
Looks like my UNION in the query is only returning distict values. More to follow.
 
I updated the query as follow:

Code:
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) ".
"or (call_history.status = \"R\" and time_connect > 120) ".
"or (call_history.status = \"CB\" and time_connect < 180))";

and everything is good. Thank you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top