use Set::IntSpan; my $set = Set::IntSpan->new("41888, 41889, 41890, 41892, 41895"); $set = $set->complement()->intersect( Set::IntSpan->new($set->min . "-" . $set->max)); print $set->run_list(), "\n"; #### 41891,41893-41894