danield has asked for the wisdom of the Perl Monks concerning the following question:
I have this code:
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:\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>
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)?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>
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 | |
|
Re: How to substitute 0 (zero) Instead of 'n/a'
by Zed_Lopez (Chaplain) on Jan 22, 2004 at 18:55 UTC | |
|
Re: How to substitute 0 (zero) Instead of 'n/a'
by duff (Parson) on Jan 22, 2004 at 19:12 UTC | |
by ysth (Canon) on Jan 22, 2004 at 19:36 UTC | |
|
Re: How to substitute 0 (zero) Instead of 'n/a'
by toonski (Beadle) on Jan 22, 2004 at 18:43 UTC | |
by arden (Curate) on Jan 22, 2004 at 18:53 UTC |