# map{$arr[$_]++ for(eval s/\-/\.\./r)}map{s/^([^,]*),?//;$1}$TM_part1,$TM_part2; # should be: map{s/^([^,]*),?//;$arr[$_]++ for( eval $1 =~ s/\-/\.\./r )}$TM_part1,$TM_part2; # so also the oneliner can be reduced: perl -le "for($ARGV[0]=~/\-/g){my@a;map{s/^([^,]*),?//;$a[$_]++for eval$1=~s/\-/\.\./r}@ARGV;print join'-',(grep$a[$_]==2,0..$#a)[0,-1]}" "25-40,74-93,95-120,130-149" "31-47,84-99,107-123,137-151" 31-40 84-93 107-120 137-149