in reply to Removing duplicate substrings from a string - is a regex possible?

Here's a hybrid solution. I'm inclined to agree that join(grep(split())) is going to be more robust, but this might be more economical:

$_ = '/New York/Chicago/New York/Boston/Houston/Chicago/Seattle/'; my %tally; s/\/([^\/]+)/($tally{$1}++) ? '' : "\/$1"/ge; print $_; __END__ prints /New York/Chicago/Boston/Houston/Washington/

Note that this requires a leading / if it's to remove duplicates of the first item in the list. Split handles that sort of thing better...

  • Comment on Re: Removing duplicate substrings from a string - is a regex possible?
  • Download Code