in reply to Re^3: How do you find duplicates in a string?
in thread How do you find duplicates in a string?
The hash approach is better for that, but I went ahead and re-implemented the regexp approach again anyway, just in case someone is interested in a pattern matching solution rather than an 'equal key' solution.
If you wanted to continue with the regexp approach, this solution will count the number of duplicate words. I modified the RE a little so that it would count "Antony Antony Antony" as two duplicates (Antony is repeated twice after the original). "Antony Antony Hank Antony Hank Mark" would count 3: Antony has two repeats, and Hank has one.
use warnings; use strict; my $string='LOCAL Antony 17 Antony 23 1569'; my $count; $count++ while $string =~ m/ \b([[:alpha:]]+)\b (?=.*?\b\1\b) /xg; print $count, "\n";
Dave
|
|---|