in reply to Re^2: extract relevent lines according to array
in thread extract relevent lines according to array

The trick is to use variables to memorize where you are in your processing. Either use a variable that records your "status" as a number (that is called a [no such wiki, state machine]) or use one or more variables that record different conditions (commonly called "flags"). Below is your script adapted to use a flag called $foundchrom:

my $foundchrom=0; while(my$line=<DATA>){ 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;} } }