Thanks for the response..... 

That was a mistake with the variables twice. The counter was moved in to the while 
loop.  The first argument to bind_columns( ) is an undef, due to historical reasons. 
It's no longer required if you are using DBI 1.08 or later. Without it I get errors.

I was tring to parse this info using perl. That is why I sent the email.  

I will try and explain again what I am trying to do.  
I read in a file with a list of 
rooms....Then im trying to list the room once and then count how many records match 
for the select statement and only print the building once with a count in front of 
it.. 

db:
date build  room  dept
##   211    30      22
##   211    30      22
##   444    50      05
##   1544    20      22
##   1544    20      22
##   333    30      22

so Now I have this in my output:
Current output:
For 30 :
1BLD:211
2BLD:211
3:BLD211
.
.
10BLD:211
11BLD:333
For 20 :
1BLD:1544
2BLD:1544

My goal was to have this output:
For 30 :
10BLD:211
1BLD:333
For 20 :
2BLD:1544

Thanks......

-----Original Message-----
From: Wiggins d Anconia <[EMAIL PROTECTED]>
Sent: Jun 16, 2004 7:23 AM
To: rmck <[EMAIL PROTECTED]>, [EMAIL PROTECTED]
Subject: Re: Counting help

> Im trying to list a record once and then count how many records. But I
keep getting a list of all the records and it counts those?? I read in a
file with a list of rooms and then look for each one that matches dept
22 and Im trying to show it once and then count how many for the building?
> 
> 
> #!/usr/bin/perl
> use strict;
> use DBI;
> use warnings;
>  
> my $list = "/etc/cron.d/room.txt";
> my $dbh = DBI->connect("dbi:mysql:dept","user","")
>         or die("Couldn't connect");
> my( $date, $build, $room, $dept );

Why do you declare the above variables at this scope level, and again
inside the while loop?

> my $cnt = 1;

You have initialized your counter here, which it should be reset each
time through the loop, that should fix your immediate problem. But
should you be setting this to zero, what if there are no results?

> open (LIST, "$list") or die "read error";
>  
> while (<LIST>) {
> chomp;
> print "For $_ :\n";
> my $sql = "select date,build,room,dept from Jun04 where room = '$_'
and dept = '22' order by date";

You should consider using placeholders for the above so that you don't
have to handle the quoting yourself, especially since you have already
broken the DBI steps into prepare/execute.

> my $sth = $dbh->prepare( $sql );
> $sth->execute();
> my( $date, $build, $room, $dept  );
> $sth->bind_columns( undef, \$date, \$build, \$room, \$dept );
>  

What is this 'undef' up to?

>  
> while( $sth->fetch() ) {
> print "For $room :\n";
> print $cnt++;print "BLD:$build\n";
> }
> $sth->finish();
>  }
>  
> $dbh->disconnect();
> 
> 
> goal output:
> For 30 :
> 10BLD:211
> For 20 :
> 2BLD:1544
> 
> 
> Current output:
> For 30 :
> 1BLD:211
> 2BLD:211
> 3:BLD211
> .
> .
> 10BLD:211
> For 20 :
> 11BLD:1544
> 12BLD:1544
> 

Not sure what your overall end goal is, or what DB engine you are using,
but if counting is your only goal and you have no other reason to select
specific fields you should consider letting the DB handle it.  Most
support some sort of counting ability.

http://danconia.org



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to