in reply to Counting lines as I write them...

So count them as you print them.

my %count; ## ... if( "!!!" eq substr $line => 0, 3 ) { $count{ "!!!" }++; print OUTFILE $line; } ## ... print "Total errors for $_: $count{ $_ }\n" for qw( @@@ *** !!! );

Update: And no one else has mentioned it, but you might want while( defined( $line = <INFILE> ) ) { ... } if 0\n might occur in your input.</pedant>

Replies are listed 'Best First'.
Re^2: Counting lines as I write them...
by Tanktalus (Canon) on Apr 11, 2005 at 15:16 UTC

    Regarding your pedant ...

    use strict; use warnings; use diagnostics; while (my $line = <DATA>) { print $line; } __DATA__ 0 1 2 3 0
    (Note that the last line does not have a carriage return on it - so the last character in the file is '0'.)

    Running this with perl 5.8.6 seems to net me all 5 lines (and no carriage return on the last line, leaving my prompt shifted over by one). perlsyn refers to this behaviour (i.e., while implicitly checking definedness rather than truth for file reads) rather off-the-cuff in its examples, but I'm having a hard time finding where this is explicit.

      Carp, now I can't remember when that changed. That was the behavior ages ago, and then I think you're right that there was an implicit defined-ness check stuck in sometime. I want to say I hit that with an older (5.6 era) perl sometime recently but I can't reproduce it with 5.6.1.

      Probably just best to remember it can happen in case you're ever messing around with a really old version, and not worry about it otherwise.