in reply to splitting headache

Hey Wibble,
In the following example I created another duplicate pair for show. The code looks at it as if the duplicates are not together, in otherwords I am assuming this could happen.
my $string = (join(".", sort split(/\./, "Pugh.Barney.Pugh.Barney.McGr +ew.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
If you need to keep the string together you can use .= in the foreach loop and add ' around the dups as well.
The following will add single quotes around the dups.
$string =~ s/(\S+)\.\1/'$1.$1'/g; # Add quotes and plugged into merlyn's reply $_ = join(".", sort split(/\./, "Pugh.Barney.Pugh.Barney.McGrew.Cuthbe +rt.Dibble.Grub")); $_ =~ s/(\S+)\.\1/'$1.$1'/g; # Add quotes my @keepers = grep defined $_, /'(.*?)'|([^.]+)/g; print map "$_\n", @keepers;
Gyro