#!/usr/bin/perl open(REFILE, 're_file.txt') || die $!; my %data1; while() { chomp; next unless $_; my($id1, $id2, $p10, $p11, $p12, $d1) = split; $data1{$id1}->{$id2} = [$id2, $p10, $p11, $p12, $d1]; # reference !! } close(REFILE); # open(PRT,">re_file2.txt"); open(DATAFILE,"re_mplus1.txt"); while() { chomp; next unless $_; my($id, $c7, $c8, $d2) = split(/\s+/, $_); foreach $datafile ( keys %{$data1{$id}}) { $d3 = abs($data1{$id}->{$datafile}[4]-$d2); if($d3 <= 5 ) { # print "$id, abs($data1{$id}->{$datafile}[4]-$d2),$d3\n"; # %myhash1 = [$data1{$id}->{$datafile}[0],$data1{$id}->{$datafile}[1],$data1{$id}->{$datafile}[2],$data1{$id}->{$datafile}[3],$data1{$id }->{$datafile}[4],$d3]; } } my @arr = sort { abs($a->[4]) <=> abs($b->[4]) } values %{$data1{$id}}; print "$id $c7 $c8 "; print join(" ", map { join(' ', @{$_}[0,1,4]) } splice(@arr, 0, 5 ) ) . "\n"; } close(DATAFILE); # close(PRT);