Hello,

I am a beginner in perl. I am stuck with a problem. I have an array which 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
``````

`Inline Code Example Here`

These are the X, Y and Z coordinates from a pdb file. I want to calculate the distance using these three coordinate values. First i want to know how to assign variable names to these values in the array. I have to use this formula to calculate the distance:

\$dist = sqrt((\$x1-\$x2)2+(\$y1-\$y2)2+(\$z1-\$z2)**2);

Till now I have written the script shown below to obtain the three columns which is shown above. I am stuck. I need help to calculate distance using 'for loop' or i guess its two for loops within which would look something like this:

``````for(\$AAi = 1; \$AAi<= 7; \$AAi++){   #here \$AAi shud take the first three values(x1, y1, z1)
for (\$AAj = 2; \$AAj<= 7; \$AAj++){   #similarly \$AAj shud take 2nd 3 values(x2, y2, z2)
my \$dist = sqrt((\$x1-\$x2)**2+(\$y1-\$y2)**2+(\$z1-\$z2)**2);
``````

and do an iterative distance calculation. am stuck....

my exiting code:

``````open(IN, "/Users/anu/out.pl") or die "\$!";
while (my \$line = <IN>) {
chomp(\$line);
my @array = (split (/\s+/, \$line))[6, 7, 8];
print "@array\n";
}
close(IN);
``````

Thank you..

## All 5 Replies

> my exiting code:
Really....
http://www.daniweb.com/forums/post702120-2.html
Try to reply to existing threads which cover basically the same topic as before.

In your loop, having worked out the distance between
6.324 32.707 50.379
5.197 32.618 46.826
ie, lines 1 and 2

Would you then need to work out the distance between
5.197 32.618 46.826
4.020 36.132 46.259
ie, lines 2 and 3

Something like

``````my @old;
open (IN, "filename.pdb") or die "\$!";
while (my \$line = <IN>) {
chomp(\$line);
my @array = (split(/\s+/,\$line))[6,7,8];
print "@array\n";
if ( \$. > 1 ) {
# now work out distance between @old and @array
}
@old = @array;
}
close (IN);``````

hi,

Well, I did get extract the three columns with the script, but i wanted to know how to calculate the distance and put them in an output file.

The distance calculation is between row1_row2, then row1_row3, row1_row4, row1_row5.... so on... till row6_row7.

This was the problem. I do not know how to do this iterative distance calcultion.

Thank you,

Don't bother, they have been given the answer on another forum:

distance calculation for XYZ coordiantes
I cant reach the link perlguru??? :(
Can any one help me?? Even i have similar type of problem as annie_singe..