I have a file that looks like this.
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
etc etc where the first column is a position number and the other columns my data of interest. I want to create a 'sliding window' whereby all the data points within 5 positions are taken into account and then print off the highest of the scores for my 5 data points within those 5 positions - for example for the data:
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
I would print out
10-14 0.57 0.07 0.62 0.32 0.58
This is what I've attempted so far - which simply doesnt work. I cant get the count to increment. Do I need to use an array instead of using a while loop to go though each line of the file one at a time?
#!/usr/bin/perl -w use strict; use warnings; use English; use FileHandle; use Exception; my $input = shift; my $count = 0; my $largest_cons1 = 0; my $largest_cons2 = 0; my $largest_cons3 = 0; my $largest_cons4 = 0; my $largest_cons5 = 0; open(FILE, "$input") || die "ERROR: Unable to open input file: $!\n"; while(<FILE>) { my @data = split(/\s+/, $_); $count++; my $pos = $data[1]; my $cons1 = $data[2]; my $cons2 = $data[3]; my $cons3 = $data[4]; my $cons4 = $data[5]; my $cons5 = $data[6]; if($count < 5) { if($cons1 > $largest_cons1) { $largest_cons1 = $cons1; } if($cons2 > $largest_cons2) { $largest_cons2 = $cons2; } if($cons3 > $largest_cons3) { $largest_cons3 = $cons3; } if($cons4 > $largest_cons4) { $largest_cons4 = $cons4; } if($cons5 > $largest_cons5) { $largest_cons5 = $cons5; } } print "$pos $largest_cons1 $largest_cons2 $largest_cons3 $largest_ +cons4 $largest_cons5\n"; }
Any help/suggestions much appreciated. Thanks in advance!

In reply to creating and printing a sliding window by Angharad

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.