#!/usr/bin/perl -w use strict; my $chrom='chr1'; my $start=9839; my $stop=9841; my @values; while ( defined(my $line=) and !@values) { @values = get_values() if ( $line =~ m/\=$chrom$/); } sub get_values { my @values; while ( defined(my $line=) ) { last unless $line =~ m/^\d/; my ($tag,$value) = split(/\s+/,$line); push (@values, $value) if ($tag >= $start and $tag <= $stop); } return @values } print "@values"; #prints: 0.007 0.004 0.002 __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 #### my @values; while () { if ( (/\=$chrom$/.../^v/) =~ m/^\d+(?= $start and $tag <= $stop); } else {last if @values} #optional } print "@values"; #prints: 0.007 0.004 0.002