On Sat, Jan 24, 2004 at 10:14:24PM -0700, danield ([EMAIL PROTECTED]) wrote:
> Hello all,
> 
> Thanks to Charles and Tim, I have advanced to the final step with my
> script. After the script verifies that there is line (Summary Log...)
> and that the log is from correct year and month. I want it to find a
> line where is " Impressions:           XX" and retrieve that value.
> However I got 0 (zero) as an result, even tough I know that there are
> values (Impressions).
> 
> The script now looks like:
> 
> #!usr/bin/perl -w
> 
> use strict;
> use Fcntl qw[:flock];
> 
> my $impressions = 0;
> my $iofile =
> '/other/XPSmaint/scripts/billing/daniel/input/c07_impressions_io.info';
> 
> open (IO, $iofile) || die("Could not open file 1!");
> 
> while (<IO> ) {
>       chop;
>      my ($FH, $output, $file2check, $month, $year) = split (/\s+/, $_);
> 
> open OUT, ">> $output";
> 
> chdir $FH or die "$!";
> 
> while (glob $file2check) {
>     open FH, $_ or die $!;
>     flock FH, LOCK_SH or die $!;
>     while (<FH> ) {
>         chomp;
>       if ( / Summary Log \(generated:/ ) {
>           my($emptyspce, $summary, $log, $generated, $day_word, $monthfile,
> $day_number, $time, $timezone, $yearfile ) = split(/\s+/, $_);
>       #print "File $file2check is $monthfile, $year\n";
>           if ($monthfile eq $month
>               and $yearfile eq $year){
>               #print "File has this year: $yearfile and month: $monthfile in it.\n"}
>               if ( /Impressions:/ ) {
>                     my($text, $value) = split(/:/, $_);
>                     $impressions += $value if ($value =~ /\d+/);
>                }
>             }
>            }
>         }
>     close FH or die $!;
>    }
> print OUT 'Total impressions: ', $impressions or die $!;
> }
> 
> Any suggestions?

The following bits of your code seem to work fine for me -
#!/usr/bin/perl
use warnings;
use strict;

open OUT, ">> output" or die $!;
my $impressions = 0;
while(<DATA>) {
    if (/Impressions:/) {
        my($text, $value) = split(/:/, $_);
        $impressions += $value if ($value =~ /\d+/);
    }
}
print OUT 'Total impressions: ', $impressions or die $!;

__DATA__
 Impressions:           23
 Impressions:           12
 Impressions:           34
__END__

I would try putting "print" in front of -
"$impressions += $value if ($value =~ /\d+/);" and verify it is spitting out
what you want.  If it doesn't I suspect the "0" comes from your global declaration
"my $impressions = 0;"  That means a problem between the global
declaration and the last line of your inner 'while' loop -
"$impressions += $value if ($value =~ /\d+/);"

Or if the print statement shows 
"$impressions += $value if ($value =~ /\d+/);" is spiting out what you
want then possibly "print OUT 'Total impressions: ', $impressions or die $!;" 
needs to be moved into you inner while loop.

Just some thoughts.
Kent

-- 

-- 
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