|
[SPOILER] Perl 'Easy' Quiz of the Week #2005-1: msg#00043lang.perl.qotw.discuss
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) #!/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 = ''; } print; } print $mline; __END__ If I were into justifying my code with grand principles, I'd point out that this is an example of the "null object" pattern (http://c2.com/cgi/wiki?NullObject) - I end up doing {print $mline} often, but when $mline is the empty string, this print statement has no effect. Therefore I don't need several extra variables to track whether I've already printed the proper thing or not, or whether I'm dealing with the first line of the file, etc. I dislike special cases, because I always get into 1-off errors when dealing with them. Using {print ''} as a no-op allows me to do away with special cases. |
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | Re: [QUIZ] Perl 'Easy' Quiz of the Week #2005-1: 00043, Brad Greenlee |
|---|---|
| Next by Date: | Re: [SPOILER] Perl 'Easy' Quiz of the Week #2005-1 (Correction): 00043, Daniel Martin |
| Previous by Thread: | [SPOILER] Perl 'Easy' Quiz of the Week #2005-1i: 00043, Brad Greenlee |
| Next by Thread: | Re: [SPOILER] Perl 'Easy' Quiz of the Week #2005-1 (Correction): 00043, Daniel Martin |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |