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

Assinging values to an array - perl beginner

P: 1
hi,
I have a program to calculate the distance. its like this:
Expand|Select|Wrap|Line Numbers
  1. open(IN, "/path/outModified.pl") or die "$!";
  2.  
  3. while (my $line = <IN>) {
  4. chomp($line);
  5. my @array = (split (/\s+/, $line))[6, 7, 8];
  6. # print "@array\n";
  7. push @points, [ @array ];
  8. }
  9.  
  10. close(IN);
  11.  
  12. print '@points : ', Dumper \@points;
  13.  
  14. open my $out_file, '>', "/path/dist_modified.pl" or die "cannot open: $!\n";
  15.  
  16. for my $i1 ( 0 .. $#points -1 ){
  17. my ( $x1, $y1, $z1 ) = @{ $points[$i1] };
  18. for my $i2 ( 1 .. $#points){
  19. my ( $x2, $y2, $z2 ) = @{ $points[$i2] };
  20. my $dist = sqrt(($x2 - $x1)**2 + ($y2 - $y1)**2 + ($z2 - $z1)**2);
  21. print $out_file "153L.pdb: the distance between CA $i1 and CA $i2 = $dist\n";
  22. }
  23. }
  24. close $out_file or die "cannot close file: $!\n";
  25.  
And the distances($dist) looks like this

6.324 32.707 50.379
5.197 32.618 46.826
4.020 36.132 46.259
7.131 38.210 45.919
6.719 38.935 42.270
2.986 39.221 41.892
-0.269 37.184 41.565
-1.140 35.549 38.341
-4.817 35.710 39.211
-4.704 39.489 39.013

my question is, i have to compare these $dist values with another value in another file (DBins.pl)which has the data:

col1 col2
1 2.69
2 2.97032
3 3.25064
4 3.53096
5 3.81128
6 4.0916
7 4.37192
8 ....... so on.....
.
.
170 50.98
171 51.086....so on

I have to match the value of $dist with the column 2(col2) with its approximate value and retrieve the corresponding column1(col1)value? I have to match the value of $dist to the second column value(maximum is upto 50. any value of $dist greater than 50 should stop at the value(170 50.98)and retireve the value 170 for $dist compared with 50.98 or to its nearest high value. I dont know how to write this in perl. Can you help...The first column is the index value and second column is the energy value, so the energy value shud be compared to the $dist and the index value shud be printed in output.

To make it clear, if the value of $dist = 2.84, then the program should search for second row in the file [2 2.97032] and print only 2. that is the first column.it this possible. I think i have to use regular expressions?? or pattern matching??

pls help I am a beginner in perl.

I have written the code like this after calculating the $dist:
Expand|Select|Wrap|Line Numbers
  1. open(DIST,"path/DBins.pl") or die "$!";
  2. while(my $lines = <DIST>){
  3. chomp($lines);
  4. my @point = (split (/\s+/, $lines))[1,2];
  5.  
i dnt know how to assing values to the columns which i have split. Please help


Thank you in advance
Oct 7 '08 #1
Share this Question
Share on Google+
1 Reply


nithinpes
Expert 100+
P: 410
You may use this approach:
Expand|Select|Wrap|Line Numbers
  1. my %energy;
  2. open(DIST,"DBins.pl") or die "$!"; 
  3. while(my $lines = <DIST>){ 
  4. chomp($lines); 
  5. my ($in,$val) = (split (/\s+/, $lines)); # get the index and energy value
  6. $energy{$in} = $val;
  7. }
  8. close DIST;
  9. my %diff = map { $_ => abs($dist-$energy{$_})} (keys %energy);
  10. #get the key for smallest difference
  11. my $res=(sort {$diff{$a} <=>$diff{$b} } keys %diff) [0];  
  12. print "$res"; # print the index
  13.  
  14.  
Oct 7 '08 #2

Post your reply

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