open $fh,"genome_pred_exons.bed"; use List::Util qw( min max ); while(<$fh>) { chomp; @lh = split /\t/,$_; $ph{$lh[0]."\t".$lh[1]."\t".$lh[2]} .= $lh[3]."\t".$lh[4]."NNNNNN"; } close($fh); foreach $k(sort keys %ph) { @c = sort { $a <=> $b } split ("NNNNNN",$ph{$k}); @g = (); foreach $m(@c) { @r = split /\t/,$m; if (@g == 0) { push @g, [$r[0]+0, $r[1]+0]; next; } else { for ($i = 0;$i <= $#g;$i++) { if(($r[0] + 0) <= ($g[$i][1] + 1)) { $g[$i][0] = min($g[$i][0]+0, $r[0]+0); $g[$i][1] = max($g[$i][1]+0, $r[1]+0); last; } elsif ( $i == $#g ) { push @g, [$r[0]+0, $r[1]+0]; last; } } } } } print Dumper \@g;