# 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";