Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Perl Help!

Options
  • 03-02-2012 12:48pm
    #1
    Registered Users Posts: 26


    So i'm pretty new to Perl, i've been working with R, AWK, BASH and sed for a few months now and have managed to develop a few perl scripts for my needs, how ever i have a bit of a more challenging script to produce. I have a file of pair wise individuals with a score for each pair in a separate column. However some of the pairs are compared a few times but result in different scores. I need a script that can some how add the scores together for pairs that are seen multiple times, and output these added scores with the pair ids to a new file. The head of my file looks like this.


    AAP ABG 959606
    AAP ACC 606931
    AAP ACC 210403
    AAP ACC 584506
    AAP ACC 526372
    AAP ACW 1134267
    AAP AEC 643669
    AAP AEC 836776
    AAP AHD 342686
    AAP AHD 1026724
    AAP AHD 302872
    AAP AIW 129038
    AAP AIW 431146
    AAP ALL 254395

    i.e. everytime AAP ACC is seen add the value in the third column and output it to a file with AAP ACC beside it.

    Does not need to be written in Perl, even if someone can put some sort of pseudo code up which code help me that would be great


Comments

  • Registered Users Posts: 6,505 ✭✭✭daymobrew


    If I understand the problem you want to add matching pairs e.g. add the following together:
    AAP ACC 606931
    AAP ACC 210403
    AAP ACC 584506
    AAP ACC 526372

    If so, I would read each line, split on whitespace, join the pairs to make a single string ("AAP ACC") and create a hash item for that string, adding it to the current hash element. Then write the hash to a new file.

    I don't think that you have to check that the hash key exists - you can probably just do:
    $hash{$key} += $value;


Advertisement