By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,809 Members | 1,999 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,809 IT Pros & Developers. It's quick & easy.

multiple key value hash problem

P: 55
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
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. my  (%hashname,$cnum,$ctotal,$count,$number,$pdb,$crossangle,$resname,$respos,$resdist,@temp);
  5. open(SP,"<packing_cluster.dat") or die "Could not open $!";
  6. while(<SP>)
  7. {
  8.     my $line1 = $_;chomp $line1;
  9.     @temp = split (/\s/,$line1);
  10.     $cnum       = $temp[1];
  11.     $ctotal     = $temp[2];
  12.     $count      = $temp[3];
  13.     $number     = $temp[4];
  14.     $pdb        = $temp[5];
  15.     $crossangle = $temp[6];
  16.     $resname    = $temp[7];
  17.     $respos     = $temp[8];
  18.     $resdist    = $temp[9];
  19.     $hashname{$number} = ($pdb,$crossangle,$resname,$respos,$resdist);
  20. }
  21. close(SP);
  22. my $cnt =0;
  23. #while (($number,$pdb) = each (%hashname))
  24. #{
  25. #    print "$number => $pdb\n";
  26. #    $cnt +=1;
  27. #}
  28. open(CG,"rmsd_cluster.dat") or die "Check relevant file";
  29. while(<CG>)
  30. {
  31.         my $line2 = $_; chomp $line2;
  32.         my @gsp   = split(/\s/,$line2);
  33.         if (exists $hashname{$gsp[5]})
  34.         {
  35.             print "$gsp[2]\t$gsp[3]\t$gsp[4]\t$gsp[5]\t$hashname{$gsp[5]}\t$gsp[7]\t$crossangle\n";
  36.             $cnt +=1;
  37.         }
  38. }
  39. #close(WRITE1);
  40. print "$cnt\n";
  41.  
the problem is i am not able to print the table in desired way.
Any help will be appreciated.

Thanks
Kumar
Sep 19 '08 #1
Share this Question
Share on Google+
3 Replies


eWish
Expert 100+
P: 971
May we please see some sample data?

--Kevin
Sep 19 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
at a minimum this line looks wrong:

Expand|Select|Wrap|Line Numbers
  1. $hashname{$number} = ($pdb,$crossangle,$resname,$respos,$resdist);
you want to use square brackets to assign an array to a hash key:

Expand|Select|Wrap|Line Numbers
  1. $hashname{$number} = [$pdb,$crossangle,$resname,$respos,$resdist];
thats because its actually a reference to an array.
Sep 19 '08 #3

P: 55
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
Sep 20 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.