On Jul 17, 2008, at 10:31 AM, Richard Morse wrote:
On Jul 15, 2008, at 10:29 PM, Bill Ricker wrote:
We did some specs for new main loop.
Hi! I had volunteered to write up the algorithm, and last night I
started doing that, but ran into several things we hadn't thought
of. I was able to fix most of them, although it made things a bit
more complicated, and there's still an issue for the separator.
I've attached my code, which I know sucks, but at least mostly works
(it doesn't print the separator when $before == $after == 0).
Phoo. Apparently, something stripped out my code.
I've pasted it below...
#!/usr/bin/perl
use strict;
use warnings;
my $before = 0;
my $after = 0;
my $pattern = qr/foo/;
my $buffer = [ ];
my $buffer_size = $before + 1 + $after;
# prepopulate the buffor so that the check for $in_match_context will
work
for (1..$buffer_size) {
push @$buffer, [ 0, '', '' ];
}
my $in_match_context = 0;
my $separator = '';
my $post_print_count = 0; # needed to make $after lines work
my $count_nonmatched_lines = 0;
while (scalar(@$buffer)) {
if (defined($buffer->[$before])) {
$in_match_context = $buffer->[$before]->[0] ||
$in_match_context;
}
my $head_line = shift @$buffer;
if ($in_match_context or $post_print_count) { # if we are near
something printing, or we are still printing $afters
print $separator;
print $head_line->[1] . ($head_line->[1] ne '' ? ($head_line-
>[0] ? ':' : '-') : '') . $head_line->[2];
$separator = '';
$post_print_count--;
if ($post_print_count == 0) { $separator = "--\n"; } # this
doesn't work properly if $after = $before = 0
if ($post_print_count < 0) { $post_print_count = 0; }
if ($head_line->[0]) { $post_print_count = $after; }
}
my $curr_line_is_match = 0;
my $input_line = <DATA>;
my $curr_line;
if (defined($input_line)) {
$curr_line_is_match = ($input_line =~ m/$pattern/) || 0;
$curr_line = [ $curr_line_is_match, $., $input_line ];
}
if ($curr_line_is_match) {
$count_nonmatched_lines = 0;
}
else {
$count_nonmatched_lines++;
}
if ($in_match_context and ($count_nonmatched_lines >=
$buffer_size)) {
$in_match_context = 0;
}
if (defined($curr_line)) {
push @$buffer, $curr_line;
}
}
__END__
x
y
foo
x
n
b
foo
y
z
v
s
1
2
3
4
5
6
foo
v
a
b
c
The information transmitted in this electronic communication is intended only
for the person or entity to whom it is addressed and may contain confidential
and/or privileged material. Any review, retransmission, dissemination or other
use of or taking of any action in reliance upon this information by persons or
entities other than the intended recipient is prohibited. If you received this
information in error, please contact the Compliance HelpLine at 800-856-1983 and
properly dispose of this information.
_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm