http://qs1969.pair.com?node_id=359469

# This code gets the overlap between two closed ranges: # R1 = [$L1, $U1] # and R2 = [$L2, $U2] # parameters my $L1 = 5; my $L2 = -33; my $U1 = 10; my $U2 = 10; # result my $common; # if R1 ends somewhere in R2 if (($U1 >= $L2) && ($U1 <= $U2)) { # make sure to use smallest lower boundary ($L1 > $L2) ? ( $common = $U1 - $L1) : ($common = $U1 - $L2); $common++; } # elsif R2 ends somewhere in R1 elsif (($U2 >= $L1) && ($U2 <= $U1)) { # make sure to use smallest lower boundary ($L1 > $L2) ? ( $common = $U2 - $L1) : ($common = $U2 - $L2); $common++; } # else no match else { $common = 0; } print "Common: $common\n";