Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: filtering data and while loop problem

by nvivek (Vicar)
on Apr 20, 2010 at 05:27 UTC ( #835654=note: print w/replies, xml ) Need Help??

in reply to filtering data and while loop problem

You try this,it will work.
use strict; use warnings; use Data::Dumper; my @triples = ("chr1 9837 9840", "chr1 99998 99999", "chr2 9838 9840") +; my($start,$chrom,$stop,$avg); foreach my $triple (@triples){ print "$triple :"; ($chrom,$start,$stop)=split(/\s+/,$triple); my @values=(); while ( defined(my $line=<DATA>) and !@values ) { if ( $line =~ m/$chrom/) { @values=get_values(); if(@values) { average(\@values); print "Average:",average(\@values),"\n +" ; } } } print "Not Found:Average NA\n" unless (@values); seek DATA,0,0; } sub get_values { my @values; while ( defined(my $line=<DATA>) ) { last unless $line =~ m/^\d/; my ($tag,$value) = split(/\s+/,$line); push (@values, $value) if ($tag >= $start and $tag <= +$stop); } return @values } sub average { my ($array_ref) = @_; my $sum; my $count = scalar @$array_ref; foreach (@$array_ref) { $sum += $_; } return $sum / $count; } __DATA__ variableStep chrom=chr1 9837 0.010 9838 0.008 9839 0.007 9840 0.004 9841 0.002 9842 0.001 variableStep chrom=chr2 9837 0.090 9838 0.038 9839 0.017 9840 0.044 9841 0.052 9842 0.091

Replies are listed 'Best First'.
Re^2: filtering data and while loop problem
by ikegami (Pope) on Apr 20, 2010 at 05:39 UTC
    DATA is the handle Perl uses to parse the file. seek DATA,0,0; seeks back to the #! line. You're seeking way too far back. You'd need to tell the starting position and seek to that position.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://835654]
[erix]: and what's the "Vineyard"? Don't tell me it's doing supernatural stuff too.
[shmem]: go figure. Might help to open views to other ways of perceiving reality. No, no supernatural stuff.
[shmem]: the "Vineyard" is a biblic term, also. But that's not the point.
[erix]: sorry, my attention is a rar commodity. I will not squander it on such pro-russia sites :)
[erix]: *rare commodity
[shmem]: erix: you are utterly mistaken in marking that site as "pro-russian".
[erix]: how do you mean? the bear, explanations of Putin's "election", the fearsome new russian weaponry. I came across them immediately. Just coincidence?

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2018-03-19 21:01 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (246 votes). Check out past polls.