my $string = (join(".", sort split(/\./, "Pugh.Barney.Pugh.Barney.McGrew.Cuthbert.Dibble.Grub"))); my @array = ($string =~ m/(\S+)\.\1/g); # Capture duplicate $string =~ s/(\S+)\.\1//g; # Eliminate duplicates $string =~ s/\./\n/g; # Change \. to \n foreach (@array) { # Print duplicates print "\n$_.$_"; } print "$string"; # Print the rest of the list #### $string =~ s/(\S+)\.\1/'$1.$1'/g; # Add quotes and plugged into merlyn's reply $_ = join(".", sort split(/\./, "Pugh.Barney.Pugh.Barney.McGrew.Cuthbert.Dibble.Grub")); $_ =~ s/(\S+)\.\1/'$1.$1'/g; # Add quotes my @keepers = grep defined $_, /'(.*?)'|([^.]+)/g; print map "$_\n", @keepers;