my $string = "foo bar ..."; my %L; map($L{$_}++, split(/\s/, $string)); # bonus: each hash element has the number of occurrences as value. $string = join(' ', keys(%L));