in reply to Calculate the overlap length between two ranges
The math is simple if you have min and max from List::Util:
use strict; use warnings; use List::Util qw(min max); my $range1 = "31-52"; my $range2 = "27-50"; print max(min($2,$4)-max($1,$3)+1,0),"\n" if "$range1,$range2"=~/(\d+) +-(\d+),(\d+)-(\d+)/;
Of course, each max or min could be replaced by the ternary operator ?:.
|
|---|