Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<ol> <li><p>Avoid naked handles when possible; use $fh (filehandle) instead of FH</p></li> <li><p>You can use until instead of unless, and skip the redo:</p> <pre><code>print "Enter the file name\n"; my $file_a = &lt;STDIN&gt;; chomp $file_a; my $fh_a; until(open $fh_a, '&lt;', $file_a) { print "Re-enter the file name or 'n' to cancel\n"; $file_a = &lt;STDIN&gt;; chomp $file_a; if($file_a eq 'n') { exit; } } </code></pre></li> <li><p>You can (should) use an array instead of all those individual column variables: <code>my @cols_a = split /\t/, $line;</code></p></li> <li><p>You should read file B into an array, once, and then search that array each time you need to: <code>my @file_b = &lt;$fh_b&gt;;</code></p></li> <li><p>The result will look something like this:</p> <pre><code>#Assume we have opened both files already . . . my @file_b = &lt;$fh_b&gt;; chomp @file_b; while(my $line = &lt;$fh_a&gt;) { chomp $line; my @cols_a = split /\t/, $line; #Remember, most arrays (perl included) are zero-indexed, #so $cols_a[1] is actually the SECOND column. my $range = ($cols_a[1] + length $cols_a[3]); foreach my $line_b (@file_b) { #This loop will run once for every single line of file A. #Not efficient, but it will work. #There are, of course, lots of optimisations you can make #(starting with, for example, storing file B as an array of array #references so you don't have to split each line every time) my @cols_b = split /\t/, $line_b; if($cols_b[3] &gt; $cols_a[2] &amp;&amp; $cols_b[3] &lt; ($cols_a[2] + $range)) { #Do whatever here } } } </code></pre></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload