Re: [SPOILER] Perl 'Easy' Quiz of the Week #2005-1 (Correction)

Daniel Martin <martin-+m399P62/[email protected]> Mon, 24 Jan 2005 13:19:28 -0500 (EST)
Newsgroups gmane.comp.lang.perl.qotw.discuss
Message-ID <[email protected]>
On Mon, 24 Jan 2005, Daniel Martin wrote:

> Huh.  No one so far seems to have hit upon the solution I hit upon, which 
> was:  (I've deliberately left out most error checking since I want the main 
> script body uncluttered)

And wouldn't you know it, I left out a piece.  Here's a corrected script:

#!/usr/bin/perl
use strict;

open(STDIN, '<', shift) if @ARGV;
open(STDOUT, '>', shift) if @ARGV;

my $mline='';
my $prefix='';

while(<>) {
   /^(\w+)\.[A-Z]$/ or die "Bad line: $_";
   if ($prefix ne $1) {
     $prefix = $1;
     print $mline;
     $mline = "$prefix.M\n";
   }
   if ($mline lt $_) {
     print $mline;
     $mline = '';
   } elsif ($mline eq $_) {
     $mline='';
   }
   print;
}
print $mline;
__END__