my $foundchrom=0; while(my$line=){ if ($line=~m/^variableStep/) { if ($line=~m/$chrom/){ $foundchrom=1; } else { $foundchrom=0; } #above if-then-else could be written shorter as #$foundchrom= ($line=~m/$chrom/); next; } my ($s,$prob)=split(/\s+/,$line); if ($s<$start or $s>$stop or not $foundchrom) { next; } else{push @values,$prob;} } }