I am a beginner on Perl and I have to do some data cleaning. Just to sump up quickly what I am doing I have a list of ID (Horse_ID), each ID is for one horse. The Horses competed in several races, that's why their ID is printed on several lines (so each line matches with one competition). For each race they competed with a different trainer (or not).
Original data to show exactly with what I am dealing with
I would like to store in an hash the names of the different trainers for each horses, and the number of the different trainers each horse have.
After have consulted some post on stack overflow, I built a code. But my code prints only the first name of the trainer found and the wrong number of trainers (0 sometimes which doesn't mean anything). I can't find the mistake... This is my code :
if (($coach =~ /\w+/) && ($Horse_ID ne '')) {
if (($trainerhash -> {$Horse_ID} -> {trainerinfo}) && ($trainerhash -> {$Horse_ID} -> {trainerinfo} !~ /$Horse_ID/)) {
$trainerhash -> {$Horse_ID} -> {trainerinfo} .= "\t$coach";
my @coach = split (/\s/, $coach);
$numtrainers = $#coach+1;
}
elsif (!$trainerhash -> {$Horse_ID} -> {trainerinfo}) {
$trainerhash -> {$Horse_ID} -> {trainerinfo} = "$coach";
}
}
Trainer hash - Number of trainers & trainer names
$trainerhash -> {$Horse_ID} -> {trainerinfo} = "$numtrainers\t$coach";
If someone have an idea, it would be great... I have already tried with a for loop but the result is the same.
Thank you in advance for your help,
Aucun commentaire:
Enregistrer un commentaire