in reply to splitting headache
If you need to keep the string together you can use .= in the foreach loop and add ' around the dups as well.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
Gyro$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;
|
|---|