Here's a somewhat simpler and more idiomatic version of your code that does the same thing that may help you. Ask about anything you do not understand:
#!/usr/bin/perl -w use strict; my( @arrayx, @arrayy, @arrayz ); while (<>) { # Find x, y, z coordinates and store in separate arrays if ($_ =~ /^ATOM/) { my @line = $_ =~ m/^(.....).(.....).(....).(...)..(....)....(. +.......)(........)(........)/; ## using push mean you don't have to count because ... push @arrayx, $line[5]; push @arrayy, $line[6]; push @arrayz, $line[7]; } } close *ARGV; ## prevent confusing error message suffixes # Calculate distance between all atom coordinates ## ... $#xxx gives you the highest index in array @xxx foreach my $i ( 0 .. $#arrayx ) { foreach my $j ( $i + 1 .. $#arrayx ) { my $dist = sqrt( ($arrayx[$i] - $arrayx[$j])**2 + ($arrayy[$i] - $arrayy[$j])**2 + ($arrayz[$i] - $arrayz[$j])**2 ); ## Adding $i and $j to your output will let you know what that + output is. print "$i <> $j : $dist\n"; } }
In reply to Re: Calc distance between atoms in pdb file
by BrowserUk
in thread Calc distance between atoms in pdb file
by stellaparallax
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |