title: Perl: Compare Values of 2 Files & Print Values of 2nd File which fall in a particular range of values from 1st file
body: <p>I have run into a little glitch. I have 2 files which look like this:</p>
  
  <p><strong>File1</strong></p>
  
  <pre><code>chr10   179423  181499  181423  2076    +   NM_001202464    ZMYND11
  chr10   693887  696118  695887  2231    +   NR_027151   C10orf108
  chr10   694016  696382  696016  2366    +   NR_027152   C10orf108
  chr10   1032348 1034467 1034348 2119    +   NM_012341   GTPBP4
  chr10   1203707 1205930 1205707 2223    +   NR_015376   LINC00200
  </code></pre>
  
  <p><strong>File2</strong></p>
  
  <pre><code>chr10   176225
  chr10   180990
  chr10   181315
  chr10   181529
  chr10   181695
  chr10   182183
  chr10   686673
  chr10   686699
  chr10   688273
  chr10   695323
  chr10   698323
  chr10   722737
  chr10   906075
  chr10   908409
  chr10   928052
  chr10   950429
  chr10   989722
  chr10   1006348
  chr10   1010731
  chr10   1020229
  chr10   1034526
  chr10   1064089
  chr10   1103000
  chr10   1103198
  chr10   1103267
  chr10   1114980
  chr10   1135327
  chr10   1150625
  chr10   1193412
  chr10   1193677
  chr10   1199817
  chr10   1212181
  chr10   1212310
  chr10   1216875
  chr10   1218919
  chr10   1226134
  chr10   1226254
  </code></pre>
  
  <p><strong>What is Required</strong></p>
  
  <p>Going row-wise, for every <code>4th element</code> from <code>File1</code> print out the values from <code>File2</code> which are <code>&gt;= 2nd element from file1 &amp; &lt;= (4th element from file1+2000)</code>  </p>
  
  <p>So for example, in <code>File1</code>, 4th element in <code>row1</code> is <em>181423</em>. From <code>File2</code>, the values which are <code>&gt;= 2nd element from file1</code> <em>(179423)</em> and <code>&lt;= 4th element from file1+2000</code><em>(183423)</em> are 18090,181315,181529,181695,182183.  </p>
  
  <p>In case no values are found, <code>NA</code> should be printed.</p>
  
  <p><strong>Desired Output</strong></p>
  
  <p>A tab delimited file which looks like this: </p>
  
  <pre><code>chr10   179423  181423  183423  NM_001202464    ZMYND11     180990
                                                              181315
                                                              181529
                                                              181695
                                                              182183
  chr10   693887  695887  697887  NR_027151   C10orf108       695323
  chr10   694016  696016  698016  NR_027152   C10orf108       695323
  chr10   1032348 1034348 1036348 NM_012341   GTPBP4          1034526
  chr10   1203707 1205707 1207707 NR_015376   LINC00200       NA  
  </code></pre>
  
  <p><strong>My Code</strong></p>
  
  <p>I am absolutely clueless as to how to go about it. Initially, I was told that I simply need to find those values from <code>file2</code> which are between the <code>2nd</code> and <code>4th</code> element of <code>file1</code> . For that , I had written the following code using hashes, which although working, was not doing the complete job. (The <code>&amp;&amp;</code> portion in the <code>if</code> loop isn't doing what I thought it should, so all the greater values are being printed)  </p>
  
  <p>Now this code is complete useless :/ I am at my wits end because I don't know if within 3 months into Perl programming, I am supposed to be able to write crafty programs.</p>
  
  <pre><code>use 5.014;
  use warnings;
  
  #Assign filenames
  my $file1 = 'file1.txt' || die $!; #File with TSS coordinates
  my $file2 = 'file2.txt' || die $!; #File with G4 coordinates
  
  #Open files
  open my $fh1, '&lt;' , $file1 || die $!;
  open my $fh2, '&lt;' , $file2 || die $!;
  
  #Open output
  open OUT, "&gt;G4_coordinates_promoters$file1.out" || die $!;
  
  #Read files
  while (&lt;$fh1&gt;) {
      chomp;
      my %data1; #Hash for TSS
      my ($key1, $val1) = (split) [1,3];
      $data1{$key1} = $val1;
      while (&lt;$fh2&gt;) {
          chomp ;
          my %data2; #Hash for G4 coordinates
          my ($key2, $val2) = (split) [1,2];
          $data2{$key2} = $val2;
  
          #Compare hashes
          if ( ($key2 &gt; $key1) &amp;&amp; 
               ($key2 &lt;&lt; $data1{$key1})){ #Here the code after &amp;&amp; is NOT working
              say OUT $key2
          }
      }
  } 
  </code></pre>
  
  <p>Thank you for going though my issue. I'd be grateful if some straightforward way could be found for approaching this problem.</p>