Note that there are some explanatory texts on larger screens.

plurals
  1. POPerl: Compare keys of 2 hashes & print the difference between the closest keys
    text
    copied!<p><strong>UPDATE(16/1/13)</strong></p> <p>Borodin pointed out another possibility which I completely overlooked.<br> In the <em>actual</em> files (I manually sat and started looking through 46 files, each about 10MB large), there are cases where for a particular value in <em>File1</em>, no <strong>smaller</strong> value exists in <em>File2</em> (but a <em>greater</em> value does).</p> <p>Likewise there exist cases where for a particular value in <em>File1</em>, no <strong>greater</strong> value exists in <em>File2</em> (but a <em>smaller</em> value does)</p> <p>I am updating the sample files and the desired output here to reflect this update.</p> <p><strong>UPDATE (15/1/13)</strong> </p> <p>I have updated the desired output to account for a case where a value in <em>File1</em> <strong>matches</strong> a value in <em>File2</em>. Thanks to Borodin for pointing out such a scenario. </p> <hr> <p>I have 2 files which look like the following :</p> <p><strong>File1</strong></p> <pre><code> chr1 10227 chr1 447989 chr1 535362 chr1 856788 chr1 249240496 </code></pre> <p><strong>File2</strong></p> <pre><code>chr1 11017 chr1 11068 chr1 23525 chr1 439583 chr1 454089 chr1 460017 chr1 544711 chr1 546239 chr1 856788 chr1 249213429 chr1 249214499 chr1 249239072 </code></pre> <p>What I need to do is that foreach value in <em>file1</em>, eg. <code>10227</code>, find from <em>file2</em> , <strong>two</strong> values which are closest. One of these values would be bigger, and the other smaller. So taking <code>10227</code> in <em>file1</em>, the values which are closest in <em>file2</em> are <code>9250</code> and <code>11017</code>. Now the difference needs to be computed viz <code>9250 - 10227</code> = <code>-977</code> and <code>11017 - 10227</code> = <code>790</code> to give an output like the following (tab delimited) :</p> <p><strong>Desired Output</strong></p> <pre><code>chr1 10227 No 790 No Match chr1 447989 No 6100 -8406 chr1 535362 No 9349 -75345 chr1 856788 Yes chr1 249240496 No No Match -25997 </code></pre> <p>I figured the fastest way to do this would be to use a hash to read in the 2 files, taking the numbers as <code>keys</code> and assigning <code>1</code> as value. </p> <p>The code I have written so far is giving the difference of <code>10227</code> wrt all the values in <em>file2</em>. Similarly with <code>447989</code> and <code>535682</code>. How do I stop this and find the difference of only the closest numbers, one which is ><code>10227</code> and one which is &lt;<code>10227</code></p> <p><strong>Code</strong></p> <pre><code>use 5.014; use warnings; #code to enter lsdpeak and pg4 data into hash with KEYS as the numerical values, VALUE as 1 #Assign filename my $file1 = 'lsdpeakmid.txt'; my $file2 = 'pg4mid.txt'; #Open file open my $fh1, '&lt;', $file1 or die $!; open my $fh2, '&lt;', $file2 or die $!; #Read in file linewise my %hash1; while(&lt;$fh1&gt;){ my $key1 = (split)[1]; $hash1{$key1} = 1; } my %hash2; while(&lt;$fh2&gt;){ my $key2 = (split)[1]; } foreach my $key1 (sort keys %hash1){ foreach my $key2 (sort keys %hash2){ say $key2-$key1; } } #Exit exit; </code></pre> <p>Thank you for taking the time to go through the problem. I would be grateful for any and every comment/answer.</p>
 

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