multiple key value hash problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kumarboston
    New Member
    • Sep 2007
    • 55

    multiple key value hash problem

    Hi All,

    I have two tables of data, one contains 9 columns and another has 7 columns,
    both the tables has 3 column common, now what i am trying to do is to merge the two tables in one using hash.
    I used the first tables as the hash and made 4th column as the key and rest 5,6,7,8,9 as the values and now i read the another table and if the values existes then print the information from both the tables.
    here is my code
    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    my  (%hashname,$cnum,$ctotal,$count,$number,$pdb,$crossangle,$resname,$respos,$resdist,@temp);
    open(SP,"<packing_cluster.dat") or die "Could not open $!";
    while(<SP>)
    {
        my $line1 = $_;chomp $line1;
        @temp = split (/\s/,$line1);
        $cnum       = $temp[1];
        $ctotal     = $temp[2];
        $count      = $temp[3];
        $number     = $temp[4];
        $pdb        = $temp[5];
        $crossangle = $temp[6];
        $resname    = $temp[7];
        $respos     = $temp[8];
        $resdist    = $temp[9];
        $hashname{$number} = ($pdb,$crossangle,$resname,$respos,$resdist);
    }
    close(SP);
    my $cnt =0;
    #while (($number,$pdb) = each (%hashname))
    #{
    #    print "$number => $pdb\n";
    #    $cnt +=1;
    #}
    open(CG,"rmsd_cluster.dat") or die "Check relevant file";
    while(<CG>)
    {
            my $line2 = $_; chomp $line2;
            my @gsp   = split(/\s/,$line2);
            if (exists $hashname{$gsp[5]})
            {
                print "$gsp[2]\t$gsp[3]\t$gsp[4]\t$gsp[5]\t$hashname{$gsp[5]}\t$gsp[7]\t$crossangle\n";
                $cnt +=1;
            }
    }
    #close(WRITE1);
    print "$cnt\n";
    the problem is i am not able to print the table in desired way.
    Any help will be appreciated.

    Thanks
    Kumar
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    May we please see some sample data?

    --Kevin

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      at a minimum this line looks wrong:

      Code:
      $hashname{$number} = ($pdb,$crossangle,$resname,$respos,$resdist);
      you want to use square brackets to assign an array to a hash key:

      Code:
      $hashname{$number} = [$pdb,$crossangle,$resname,$respos,$resdist];
      thats because its actually a reference to an array.

      Comment

      • kumarboston
        New Member
        • Sep 2007
        • 55

        #4
        hi the sample data looks like

        table 1

        cnum ctotal count number pdb crossangle resname respos resdist
        2 148 1 5265 xxx 25.3 ALA 25 6.6
        2 148 2 4414 yyy 13.6 GLY 32 5.1

        table 2
        cnum ctotal count number pdb rmsd
        2 148 1 5265 xxx 2.5
        2 148 2 4414 yyy 1.3

        now i am tying to merge the two table so that i can have one comple table rather than 2. I am trying to make number(table1) as key and rest column as values(table 1) and now matching the number (in table 2) and then merging the tables.

        thanks
        kumar

        Comment

        Working...