Hash keys are always unique. In my example, if an ID has already been seen, the new string is appended to the previous content of the value for that ID.
I could have written:
for my $line (@lines) {
$line =~ /your regex/;
if (exists $urecs{$2}) {
$urecs{$2} .= "$1,$2,$3\n";
} else {
$urecs{$2} = "$1,$2,$3\n";
}
}
but that is not necessary because Perl treats appending to an undefined value the same as appending to an empty string.
Another thing you could do: $ids{$2}++ would give you a hash of IDs seen (the keys) and how many (the values) times each was seen (again, no need to check for existence first).
As for checking the keywords, I left that out so to focus my example on the use of the hash. |