Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One way:</p> <pre><code>#!/usr/bin/perl use strict; use warnings; use List::Util qw(first); open my $fh1,'&lt;','file1' or die $!; open my $fh2,'&lt;','file2' or die $!; my %h1; while(&lt;$fh2&gt;){ chomp; my ($k,$v)=split(/\s+/); push @{$h1{$k}}, $v; } close $fh2; while (&lt;$fh1&gt;){ chomp; my ($k, $v)=split(/\s+/); my $bef=first{$_ &gt;= $v}@{$h1{$k}}; $bef=defined $bef?$bef-$v:"No match"; my $aft=first{$_ &lt;= $v}reverse @{$h1{$k}}; $aft=defined $aft?$aft-$v:"No match"; my $str=sprintf("%-8s %-10d %-5s %-8s %-8s",$k, $v,$bef?"No":"Yes",$bef?$bef:"",$aft?$aft:""); print $str, "\n"; } close $fh1; </code></pre> <p>The first <code>while</code> loop reads the second file and creates a hash where the key is chr1 and the value is an array reference containing all the values of chr1.</p> <p>The <code>foreach</code> block sorts all the keys in numerical order. The 2nd <code>while</code> loop processes the records of file1 and uses the <code>first</code> function of <code>List::Util</code> to get the results.</p> <p>The <code>first</code> function is used twice: Once, to get the first biggest value than the current value, second: to get the last smallest value than the current value which is obtained by using the <code>first</code> on a <code>reverse</code> <code>sort</code>ed array.</p> <p><strong>First function</strong>: First function returns the first number in the array which satisfies the condition.</p> <p><code>first{$_ &gt; $v}@{$h1{$k}}</code> => This gets the first number in the array which is bigger than the current number. Say for 10227, first will return 11017.</p> <p>The next thing needed is the last smallest number before 10227. To get this, the first function is applied on the reverse array. </p> <p><code>first{$_ &lt; $v}reverse @{$h1{$k}}</code> => This will return the first number which is lesser than 10227, and since the array is reversed, what we get is actually the last smallest number before 10227 which is 9250.</p> <p>On running this:</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 -1424 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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