Monday, April 23, 2012

Perl, generating new data (new hash) using two different hash tables

I've bumped into a very complicated problem (in my perspective as a newbie) and I'm not sure how to solve it. I can think of the workflow but not the script.



I have file A that looks like the following: Teacher (tab) Student1(space)Student2(space)..



Fiona       Nicole Sherry 
James Alan Nicole
Michelle Crystal
Racheal Bobby Dan Nicole


They sometimes have numbers right next to their names when there are two of the same name (ex, John1, John2). Students may also overlap if they have more than two advisors..



File B is a file that has groups of teachers together. It looks similar but the values are comma-delimited.



Fiona       Racheal,Jack
Michelle Racheal
Racheal Fiona,Michelle
Jack Fiona


The trend in file B is that a key has multiple values and each value becomes a key as well to easily find who is grouped with who.



The output I would like is which students will be likely to receive similar education based on their teacher/groups.So I would like the script to do the following:




  1. Store file A into a hash and close

  2. Open file B, go through each teacher to see if they have students (some may not, the actual list is quite big..). So if I take the first teacher, Fiona, it will look in stored file A hash table to see if there is a Fiona. If there is, (in this case, Nicole and Sherry), pop them each as new keys to a new hash table.



    while (<Group>) {
    chomp;
    $data=$_;
    $data=~/^(\S+)\s+(.*)$/;
    $TeacherA=$1;
    $group=$2;

  3. Then, look at the group of teachers who are grouped with Fiona (Racheal, Jack). Take 1 person at a time (Racheal)



    if (defined??) {
    while ($list=~/(\w+)(.*)/) {
    $TeacherB=$1;
    $group=$2;

  4. Look at file A for Racheal's students.

  5. Fill them as values (comma-delimited) for student keys made from step 2.

  6. Print student-student and teacher-teacher group.



    Nicole  Bobby,Dan,Nicole    Fiona   Racheal
    Sherry Bobby,Dan,Nicole Fiona Racheal


    Since the next teacher in Fiona's group, Jack, didn't have any students, he would not be in this results. If he had, for example, David, the results would be:



    Nicole  Bobby,Dan,Nicole    Fiona   Racheal
    Sherry Bobby,Dan,Nicole Fiona Racheal
    Nicole David Fiona Jack
    Sherry David Fiona Jack



I'm so sorry for asking such a complicated and specific question. I hope other people who are doing something like this by any chance may benefit from the answers.
Thank you so much for your help and reply. You are my only source of help.





No comments:

Post a Comment