my $hash = {}; foreach my $line ( ) { my ( $id, $type, $pos ) = split /\s+/, $line; $hash->{ $id } = { id => $id, type => $type, pos => $pos, }; } my $dup_hash = {}; foreach my $id ( keys %{ $hash } ) { my $type_pos = $hash->{ $id }{type} . '_' . $hash->{ $id }{pos}; $dup_hash->{ $type_pos }{count}++; $dup_hash->{ $type_pos }{id} = $id; }