Angharad has asked for the wisdom of the Perl Monks concerning the following question:
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: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
I would print out10 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
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?10-14 0.57 0.07 0.62 0.32 0.58
Any help/suggestions much appreciated. Thanks in advance!#!/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"; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: creating and printing a sliding window
by johngg (Canon) on Mar 04, 2009 at 15:23 UTC | |
Re: creating and printing a sliding window
by shmem (Chancellor) on Mar 04, 2009 at 15:30 UTC | |
Re: creating and printing a sliding window
by repellent (Priest) on Mar 04, 2009 at 18:48 UTC | |
Re: creating and printing a sliding window
by Utilitarian (Vicar) on Mar 04, 2009 at 15:19 UTC |