David Gerler wrote:
> 
> okay... seems tonight is the night for regex... I have been looking at the
> emails that have been flying all evening.. I have tried to implement what I
> see.
> 
> For some reason, mine don't work. Others say what they got in the email
> works. I try it and it don't.
> 
> The plan for the code is to read a count (looks like "BC0012=2") and add one
> and replace it in the file. When I execute it, It never enters the while
> loop.
> 
> Here's my code. This is the applicable part. I do close the file later. I
> have also tried opening the file with +< with any luck.
> 
> 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;
> }
> }



This should do what you want.  Adapted from:
perldoc -q increment

Found in /usr/lib/perl5/5.6.0/pod/perlfaq5.pod

               I still don't get locking.  I just want to
               increment the number in the file.  How can I do
               this?


sub count {
    use Fcntl qw(:flock :seek);
    my $file = 'count.dat';
    local( *FH, $/ );

    sysopen FH, $file, O_RDWR|O_CREAT or die "Cannot open $file: $!";
    flock FH, LOCK_EX                 or die "Cannot flock $file: $!";
    my $data = <FH>;
    seek FH, 0, SEEK_SET              or die "Cannot rewind $file: $!";
    truncate FH, 0                    or die "Cannot truncate $file:
$!";
    $data =~ s/(BC0012=)(\d+)/$1.$2+1/ie;
    print FH $data                    or die "Cannot write $file: $!";
    close FH                          or die "Cannot close $file: $!";
    }



John
-- 
use Perl;
program
fulfillment

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

Reply via email to