danield has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I have this code:

#!c:\perl\bin\perl.exe use strict; use warnings FATAL => 'all'; use Fcntl qw[:flock]; my $impressions = 0; my $FH = 'C:\\scripts\\20040120\\mr\\imp\\'; chdir $FH or die "$!"; while (glob '*Jan2004*.txt') { open FH, $_ or die $!; flock FH, LOCK_SH or die $!; while (<FH> ) { chomp; if ( /Impressions:/ ) { my($text, $value) = split(/:/, $_); print "Impressions: $value"; print "\n" #$impressions += $value; } } close FH or die $!; } print 'Total impressions: ', $impressions or die $!;<br>
This code opens file and retrieves a value assigned to 'Impressions:' I would like to caunt all values. The output so far looks like this:
C:\scripts\20040120\mr>perl c07_impressions_i.pl Impressions: 85 Impressions: 19298 Impressions: n/a Impressions: 25082 Impressions: 8314 Impressions: 8663 Impressions: 8672 Impressions: 8078 Impressions: n/a Impressions: 26167 Impressions: 7708 Impressions: 8247 Impressions: 14070 Impressions: 9641 Impressions: n/a Total impressions: 0 C:\scripts\20040120\mr>
Because there are 'n/a' I cant count the impressions together. How should I change the script so it will replace any occurence of 'n/a' with 0 (zero)?

Thank you for your time.
danield

update (broquaint): tidied up formatting

Replies are listed 'Best First'.
Re: How to substitute 0 (zero) Instead of 'n/a'
by arden (Curate) on Jan 22, 2004 at 18:41 UTC
    Use an "if digit" statement, like this:

    $impressions += $value if ( $value =~ /\d+/ );

    where you have #$impressions += $value;

    Of course, you might also want to put in the semi-colon on the print line above it, or just add the newline character to the previous print statement... :)

Re: How to substitute 0 (zero) Instead of 'n/a'
by Zed_Lopez (Chaplain) on Jan 22, 2004 at 18:55 UTC

    Well, there are a bazillion ways to do this.

    $impressions += $value unless $value =~ m{n/a};

    is one obvious way.

    I'd be more inclined toward

    while (<FH>) { if (/Impressions:/) { print; $impressions += $1 if /(\d+)/; } }
Re: How to substitute 0 (zero) Instead of 'n/a'
by duff (Parson) on Jan 22, 2004 at 19:12 UTC

    One simple way is to force perl to treat $value like a number. Since it's the string "n/a", treating it like a number will evaluate to 0. To force perl to treat it like a number, do something numeric with it. For instance:

    while (<FH> ) { chomp; if ( /Impressions:/ ) { my($text, $value) = split(/:/, $_); print "Impressions: $value"; print "\n" $value += 0; # turn $value into a number $impressions += $value; }

    HOWEVER, you're already treating $value like a number when you add it to $impressions, so if you uncomment that line it should just work without the added step of adding 0 to $value

      If you must do this, try like this instead:
      use warnings; while (<FH> ) { chomp; if ( /Impressions:/ ) { my($text, $value) = split(/:/, $_); print "Impressions: $value"; print "\n"; { no warnings "numeric"; $value += 0; # turn $value into a number } $impressions += $value; } }
Re: How to substitute 0 (zero) Instead of 'n/a'
by toonski (Beadle) on Jan 22, 2004 at 18:43 UTC
    Kind of a hack solution, but try running this script:
    #!c:\perl\bin\perl.exe use strict; use warnings FATAL => 'all'; use Fcntl qw[:flock]; my $FH = 'C:\\scripts\\20040120\\mr\\imp\\'; chdir $FH or die "$!"; while (glob '*Jan2004*.txt') { open FH, $_ or die $!; flock FH, LOCK_SH or die $!; while (<FH> ) {s/n\/a//} close FH or die $!; } print 'replacing completed'
    untested, but theoretically, that oughta work :)
      I don't think danield is wanting to replace the data in the files with zeros, just not allow the 'n/a's screw up his count. I could be wrong, in which case your solution works... :)