while (<>) { my @fields = split ','; $h{$fields[0]}{$_} = ''; } for my $id (sort keys %h) { print keys %{$h{$id}} if scalar keys %{$h{$id}} == 1; }