#!/usr/bin/perl use warnings; use strict; my %count; while (<>) { my @F1 = split /\t/; my @F2 = split /\t/, <>; my ($num, $from, $to) = (@F1[ 1, 2 ], $F2[2] ); ($from, $to) = ($to, $from) if $from > $to; ++$count{$num}{$from}{$to}; }