coldy has asked for the wisdom of the Perl Monks concerning the following question:
Summary: Id like to extract the values in the second column of <DATA> corresponding to the strings in @triples (hopefully it's apparent what I want to do from my code!)
Im having a problem with exiting "while ( defined(my $line=<DATA>) and !@values)" I think im not entering the subroutine again after the first call either.
#!/usr/bin/perl -w use strict; my @triples = ("chr1 9837 9840", "chr1 99998 99999", "chr2 9838 9840") +; my($start,$chrom,$stop); foreach my $triple (@triples){ print "$triple :"; ($chrom,$start,$stop)=split(/\s+/,$triple); my @values=(); while ( defined(my $line=<DATA>) and !@values ) { @values = get_values() if ( $line =~ m/$chrom/); if(@values) { print "average ", average(\@values), "\n"; }else {print "not found: average NA \n";$values[0]=1;} } } 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: filtering data and while loop problem
by samarzone (Pilgrim) on Apr 20, 2010 at 05:13 UTC | |
|
Re: filtering data and while loop problem
by ikegami (Patriarch) on Apr 20, 2010 at 05:37 UTC | |
|
Re: filtering data and while loop problem
by nvivek (Vicar) on Apr 20, 2010 at 05:27 UTC | |
by ikegami (Patriarch) on Apr 20, 2010 at 05:39 UTC | |
|
Re: filtering data and while loop problem
by Marshall (Canon) on Apr 20, 2010 at 12:48 UTC |