#!/usr/bin/perl use strict; use Getopt::Long; use Data::Dumper; my ($i1, $i2); GetOptions ('i=s' => \$i1, 't=s' => \$i2); open (IN, "<", $i1)|| die "$!"; open (IN1, "<", $i2)|| die "$!"; my %hash1; # set my intervals while (){ chomp; /^([^\t]*)\t([^\t]*)\t([^\t]*)/; $hash1{$1}->{$2}->{$3} =0; } close IN1; my %hash2; my $p =0; my $id = ""; #count tags per interval while(){ chomp; if (/#(.*)/){ $p=0; $id = $1; %hash2 = (); next; } $p++; # sometimes the first column is set to 0 /^(\d+)\t(\d+)/; if(exists $hash1{$id}->{$p}){ foreach my $o (keys %{$hash1{$id}->{$p}}){ $hash2{$p}->{$o}=1; } } foreach my $key1 (keys %hash2){ foreach my $key2 (keys %{$hash2{$key1}}){ $hash1{$id}->{$key1}->{$key2}++ if $2 eq '1'; delete($hash2{$key1}->{$key2}) if $p == $key2; } } } close IN; print Dumper(\%hash1);