I would stick with the while loop as it is more scalable and make use of List::Util.
use warnings;
use strict;
use List::Util qw(max);
my @window;
while (<DATA>)
{
chomp();
# create sliding window
push(@window, [ (split) ]);
shift(@window) if $. > 5;
# print range
print $window[0][0], "-", $window[-1][0];
# print maximums
for my $i (1 .. 5)
{
print " ", max(map { $_->[$i] } @window);
}
print "\n";
}
__END__
1 0 0.00 0 0 0
2 0 0.00 0 0 0
3 0 0.08 0 0 0
4 0 0.05 0 0 0
5 0 0.08 0 0 0
6 0 0.05 0 0.12 0
7 0 0.05 0 0.12 0
8 0 0.04 0 0.15 0
9 0.07 0.07 0 0.15 0.18
10 0.29 0.04 0.32 0.32 0.19
11 0.46 0.05 0.42 0.30 0.21
12 0.45 0.07 0.35 0.29 0.41
13 0.57 0.07 0.42 0.00 0.47
14 0.46 0.04 0.62 0.00 0.58
15 0.39 0.05 0.41 0.00 0.37
Output:
1-1 0 0.00 0 0 0
1-2 0 0.00 0 0 0
1-3 0 0.08 0 0 0
1-4 0 0.08 0 0 0
1-5 0 0.08 0 0 0
2-6 0 0.08 0 0.12 0
3-7 0 0.08 0 0.12 0
4-8 0 0.08 0 0.15 0
5-9 0.07 0.08 0 0.15 0.18
6-10 0.29 0.07 0.32 0.32 0.19
7-11 0.46 0.07 0.42 0.32 0.21
8-12 0.46 0.07 0.42 0.32 0.41
9-13 0.57 0.07 0.42 0.32 0.47
10-14 0.57 0.07 0.62 0.32 0.58
11-15 0.57 0.07 0.62 0.30 0.58
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|