$ perl -Mstrict -Mwarnings -E ' say join q{-}, do { my %seen; grep { not $seen{ $_ } ++ } split m{-}; } for qw{ 12-12 12-12-12 12-13-12-13 12-12-13-13 12-13-13-14 };' 12 12 12-13 12-13 12-13-14 $