I thought that was the applicable part. :-) Guess I needed more.... After the while 
loop, I would write the output to Count according to whether it was found or not. If 
it was found it should substitute the new cont for the old. If not found, it adds it 
to the end. 


After getting your email and making some changes, it will enter the while loop now and 
everything works except the print statements.

When I watch $_, the substitution takes place. It just doesn't get printed bank to 
file. Likewise, if it is not found and it executes the if (!$found) but doesn't print 
to COUNT either.

The file does contain multiple lines.

Here's the new whole sub:

sub count { 
open (COUNT, "+<./count.dat") or die "cannot open countfile: $!"; 
flock(COUNT, 2); 
my $found = 1;
my $image = "BC0012";
while (<COUNT>){   
        print "test";
        if (m/$image/i){
                ($key, $count) = split('=',$_);
                $count++;
                s/$image.*\n/$image\=$count\n/;
                print COUNT $_;
                last;
        } else {
                $found = 0;
        }
                
}
if (!$found){
        print COUNT "$image\=$count\n";
}

flock(COUNT, 8); 
close (COUNT) or die "cannot close countfile: $!"; 
return 1; 
} 

-----Original Message-----
From: Connie Chan [mailto:[EMAIL PROTECTED]]
Sent: Saturday, July 27, 2002 4:57 AM
To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
Subject: Re: filehandles and while loops


> > sub count {
> > open (COUNT, "+>./count.dat") or die "cannot open countfile: $!";
> > flock(COUNT, 2);
> > 
> > while (<COUNT>){
> >         if (m/BC0012/i){
> >                 ($key, $count) = split('=',$_);
> >                 $found = 1;
> >                 $count++;
> >         } else {
> >                 $found = 0;
> > }
> > }

That's quite strange.... is this COUNT file single line ?
or mulitiple lines ? 

If single line :
1. The while loop is useless. simplily :

    $ln = <COUNT> ;
    ($key, $count) = split(/=/, $ln) if ($ln =~ /BC0012/i);
    $found = 1 if ($key);
  
2. However, you didn't do anything to write to the FH and/or
printout the $count ?! How can you know this doesn't work ? =)

If muliple lines :

You will make $found = 0 at the end, if /BC0012/ not existed
at the last line. Maybe you want something like this.

my $found =0 ;

while (...) 
    { # if match, do something
        $found = 1
    }

print $found; # so you get the right ans.

Both single and multiple lines :
1. use strict and warnings;
2. open (FH, ">file") or +< rather then +>, that talking about 
some file mode or fix length. It's no use here. But for this 
case, I still recommand you use the simplest, >.

Rgds,
Connie




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to