use strict; use warnings; my @strings = qw( 12-12 12-12-12 12-13-12-13 12-12-13-13 12-13-13-14 ); for (@strings) { my $prev = -1; # numbers being separated by dashes, I assume that a negative value is not possible print join "-", grep {$prev != $_ and $prev = $_} sort {$a <=> $b} split /-/, $_; print "\n"; }