in reply to capturing duplicates

Your code as written could use

chomp; my $name = $_;

after the

while (<DATA>) {

(or just use $_ instead of $name.) With that, it works as you want. Without that it does nothing. So you probably made an error when you cut and pasted your code.

If you've still got questions, could you post the code as you're running it, and the data with which it's failing?