in reply to most efficient regex to delete duplicate words

This will scan the string once, with no backtracking:
# Assuming space delimited words and removing duplicates # not necessarily next to each other my $str = "abc def abc hello def 123 abc"; my %count; $str =~ s/((\w+)\s?)/$count{$2}++ ? '' : $1/eg; print $str,"\n";

Replies are listed 'Best First'.
Re: Re: most efficient regex to delete duplicate words
by coolmichael (Deacon) on Aug 14, 2001 at 06:44 UTC
    You need to be careful with that one, because it will mess with words that are repeated but aren't consecutive. It isn't clear if the poster wanted to trash only repeated consecutive words or all repeated words.

    Michael.

    Update:Right in front of me, and I still didn't read it right. Sorry runrig