 <p>To get the results you want without changing your existing code too much, you can get the sequence line when you're processing the header line:</p> <pre><code>while ( my $line = &lt;FILE2&gt; ) { if ($line =~ /^&gt;(\S+)\s+\S+\s+(\d+)\s+(\d+)\s+(\S+)/) { my $cds1 = $1; my $cds2 = $2; my $cds3 = $3; my $cds4 = $4; # fetch the next line from the file -- i.e. the sequence $nextline = &lt;FILE2&gt;; for my $cc22 (@file1list) { if ( $cc22 &gt; $cds2 &amp;&amp; $cc22 &lt; $cds3 ) { print "$cc22 $cds2 $cds3 $cds4 $nextline"; } } } } </code></pre>