>>>>> "Randal" == Randal L Schwartz <[EMAIL PROTECTED]> writes:
Randal> This is the simplest I could come up with in a few minutes. Hope it helps.
Randal> my @stretch;
Randal> while (<>) {
Randal> my ($line, $val, $char) = split;
Randal> if ($val < 1) {
Randal> push @stretch, $char;
Randal> }
Randal> if (@stretch >= 9 and ($val >= 1 or eof())) {
Randal> printf "stretch%d-%d %s\n", $.-@stretch+1, $., join "", @stretch;
Randal> @stretch = ();
Randal> }
Randal> }
And nastily broken. Too many cross conditions, and I bolluxed it.
here's a better one...
use strict;
my @stretch;
while (<>) {
my ($line, $val, $char) = split;
if ($val < 1) {
push @stretch, $char;
}
if (@stretch >= 9 and ($val >= 1 or eof())) {
printf "stretch%d-%d %s\n", $.-@stretch+1, $., join "", @stretch;
}
if ($val >= 1) {
@stretch = ();
}
}
See... the broken one was clearing out @stretch only when it was also
being dumped, but it need to be cleared out whenver $val went above 1.
Bleh.
Hmm. I just saw another simplification:
use strict;
my @stretch;
while (<>) {
my ($line, $val, $char) = split;
if ($val < 1) {
push @stretch, $char;
}
if ($val >= 1 or eof()) {
if (@stretch >= 9) {
printf "stretch%d-%d %s\n", $.-@stretch+1, $., join "", @stretch;
}
@stretch = ();
}
}
I still don't like having to test $val twice. That's messy. It
smells somehow. Can someone else figure out how to do it without
duplicating any of the tests? I can't get enough state in the program
counter. :)
Of course, there's a completely sideways solution:
use strict;
my $run = "";
my $chars = "";
while (<>) {
my ($line, $val, $char) = split;
$chars .= $char;
$run .= $val < 1 ? "Y" : "N";
}
while ($run =~ /Y{9,}/g) {
printf "stretch%d-%d %s\n", length($`)+1, length($`)+length($&),
substr($chars, length($`), length($&));
}
Oh yeah! There's some part of me that really likes that solution much
better.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[EMAIL PROTECTED]> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!