CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
calculate distance using values in an array

 



Anu Singh
Novice

Oct 1, 2008, 2:11 AM

Post #1 of 16 (4965 views)
calculate distance using values in an array Can't Post

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

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 these values to 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 like below to obtain the three columns which is shown above. I am stuck. I need help to calculate distance using 'for loop' and do an iterative distance calculation.

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...


shawnhcorey
Enthusiast


Oct 1, 2008, 5:25 AM

Post #2 of 16 (4958 views)
Re: [Anu Singh] calculate distance using values in an array [In reply to] Can't Post


In Reply To
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

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 these values to 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 like below to obtain the three columns which is shown above. I am stuck. I need help to calculate distance using 'for loop' and do an iterative distance calculation.


You haven't specified what two points you want. Do you want to pair them, that is, find the distances for points in line 1 and line 2, then line 3 and line 4, etc? Or do you want to use one as a base and find the distances from it to the others as in line1 and line2, line 1 and line 3, line 1 and line 4, etc?

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


Anu Singh
Novice

Oct 1, 2008, 5:52 AM

Post #3 of 16 (4956 views)
Re: [shawnhcorey] calculate distance using values in an array [In reply to] Can't Post

hello,

Its between two points but iteratively, for ex: its between row1_row2, row1_row3, row1_row4..... so on.... and then it shud be row2_row3, row2_row4, row2_row5......so on till row6_row7.. so i think we shud write a for loop which looks something like this

for($AAi = 1; $AAi<= 7; $AAi++){
for ($AAj = 2; $AAj<= 7; $AAj++){

then calculate distance using

my $dist = sqrt(($Xi-$Xj)**2+($Yi-$Yj)**2+($Zi-$Zj)**2)

it is supposed to be something like this...


shawnhcorey
Enthusiast


Oct 1, 2008, 6:18 AM

Post #4 of 16 (4953 views)
Re: [Anu Singh] calculate distance using values in an array [In reply to] Can't Post


In Reply To
hello,

Its between two points but iteratively, for ex: its between row1_row2, row1_row3, row1_row4..... so on.... and then it shud be row2_row3, row2_row4, row2_row5......so on till row6_row7.. so i think we shud write a for loop which looks something like this

for($AAi = 1; $AAi<= 7; $AAi++){
for ($AAj = 2; $AAj<= 7; $AAj++){

then calculate distance using

my $dist = sqrt(($Xi-$Xj)**2+($Yi-$Yj)**2+($Zi-$Zj)**2)

it is supposed to be something like this...



Code
#!/use/bin/perl 

use strict;
use warnings;

use Data::Dumper;

my @points = ();
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";
push @points, [ @array ];
}
close(IN);

print '@points : ', Dumper \@points;

for my $i1 ( 0 .. $#points -1 ){
my ( $x1, $y1, $z1 ) = @{ $points[$i1] };
for my $i2 ( 1 .. $#points ){
my ( $x2, $y2, $z2 ) = @{ $points[$i2] };
my $dist = sqrt( ($x2-$x1)**2 + ($y2-$y1)**2 + ($z2-$z1)**2 );
print "distance from ( $x1, $y1, $z1 ) to ( $x2, $y2, $z ) is $dist\n";
}
}


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


Anu Singh
Novice

Oct 1, 2008, 7:07 AM

Post #5 of 16 (4952 views)
Re: [shawnhcorey] calculate distance using values in an array [In reply to] Can't Post

Hello,

Thank you so much for that. it worked. how do i put the result to an output file? like do i have to create a file first? how do i put them to that file.

Nevertheless. thank you so much for that help

Thank you,


shawnhcorey
Enthusiast


Oct 1, 2008, 7:15 AM

Post #6 of 16 (4950 views)
Re: [Anu Singh] calculate distance using values in an array [In reply to] Can't Post


In Reply To
Hello,

Thank you so much for that. it worked. how do i put the result to an output file? like do i have to create a file first? how do i put them to that file.

Nevertheless. thank you so much for that help

Thank you,



Code
 
open my $out_fh, '>', 'distances.txt' or die "cannot open distances.txt: $!\";

for my $i1 ( 0 .. $#points -1 ){
my ( $x1, $y1, $z1 ) = @{ $points[$i1] };
for my $i2 ( 1 .. $#points ){
my ( $x2, $y2, $z2 ) = @{ $points[$i2] };
my $dist = sqrt( ($x2-$x1)**2 + ($y2-$y1)**2 + ($z2-$z1)**2 );
print $out_fh "distance from ( $x1, $y1, $z1 ) to ( $x2, $y2, $z ) is $dist\n" or die "cannot print to distances.txt: $!\n;
}
}

close $out_fh or die"cannot close distances.txt: $!\n";


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


KevinR
Veteran


Oct 1, 2008, 10:39 AM

Post #7 of 16 (4944 views)
Re: [Anu Singh] calculate distance using values in an array [In reply to] Can't Post

"my exi(s)iting code"? Don't you mean:

here is the code someone wrote for me on another forum....
-------------------------------------------------


Anu Singh
Novice

Oct 1, 2008, 2:15 PM

Post #8 of 16 (4932 views)
Re: [KevinR] calculate distance using values in an array [In reply to] Can't Post

Offcourse.. thats wat i meant...My existing code = code got from another forum which i applied in ma program.

Thank u


KevinR
Veteran


Oct 1, 2008, 2:39 PM

Post #9 of 16 (4928 views)
Re: [Anu Singh] calculate distance using values in an array [In reply to] Can't Post

It pays to be clear because so far between this forum and another, you have had your work done for you by generous strangers. Saying "my existing code" implies you wrote it, which you didn't. Hopefully you give credit to the people that helped you work on your assignment and not claim it as your own honest and diligent work, which up to this point, it isn't.

Regards,
Kevin
-------------------------------------------------


sycoogtit
User

Oct 1, 2008, 6:34 PM

Post #10 of 16 (4923 views)
Re: [KevinR] calculate distance using values in an array [In reply to] Can't Post

Amen. This sounds oddly like a homework assignment too.

--
http://bunsooter.com


KevinR
Veteran


Oct 2, 2008, 12:55 AM

Post #11 of 16 (4910 views)
Re: [shawnhcorey] calculate distance using values in an array [In reply to] Can't Post

hehehe......

http://www.programmingforums.org/thread16631.html

I guess they want as many opinions as they can get. Thats at least three forums this person has posted the same question to.

Where was the internet when I was in school! Crazy
-------------------------------------------------


Anu Singh
Novice

Oct 12, 2008, 2:14 PM

Post #12 of 16 (4845 views)
Re: [shawnhcorey] calculate distance using values in an array [In reply to] Can't Post

I had a small doubt here:

If the two values of $i1 and $i2 co incide, it prints result as 0. i.e

If($i1==$i2), how do i iterate the value of $i2 to the next value. I tried writing

$i2++ if$i1==$i2;

but this doesnt work.


KevinR
Veteran


Oct 12, 2008, 4:22 PM

Post #13 of 16 (4838 views)
Re: [Anu Singh] calculate distance using values in an array [In reply to] Can't Post

do it like this:


Code
if ($i1==$i2) { 
$i2++;
}

-------------------------------------------------


Anu Singh
Novice

Oct 13, 2008, 1:28 AM

Post #14 of 16 (4815 views)
Re: [KevinR] calculate distance using values in an array [In reply to] Can't Post

It doesnt work. I tried that.

for my $i1 ( 0 .. $#points -1 ){
my ( $x1, $y1, $z1 ) = @{ $points[$i1] };
for my $i2 ( 1 .. $#points){
my ( $x2, $y2, $z2 ) = @{ $points[$i2] };
if ($i1==$i2){
$i2++;
my $dist = sqrt(($x2 - $x1)**2 + ($y2 - $y1)**2 + ($z2 - $z1)**2);

}

it shows the result as:

$i1 $i2
CA 1 and CA 2 = 0 #it just changes the name of $i2 from 1 to 2, but it actually doesnt increase the $i2
CA 1 and CA 2 = 3.74900173379527
CA 1 and CA 3 = 5.98610633049564


KevinR
Veteran


Oct 13, 2008, 10:25 AM

Post #15 of 16 (4798 views)
Re: [Anu Singh] calculate distance using values in an array [In reply to] Can't Post

well, go back to trying it like you had it:


$i2++ if ($i1 == $i2);
-------------------------------------------------


mamat
Novice

Dec 9, 2009, 3:22 AM

Post #16 of 16 (4276 views)
Re: [shawnhcorey] calculate distance using values in an array [In reply to] Can't Post

Hi everyone,
I also trying to calculate the distance between co-ordinates, but with some variation, which I am struggling to implement. The co-ordinates I have are of the following type:

Quote
6.324 32.707 0.0000
5.197 32.618 0.0000
4.020 36.132 0.0000
7.131 38.210 0.0000
6.719 38.935 0.0000
2.986 39.221 0.0000
0.269 37.184 0.0000


However, unlink the parent poster in my case I need to calculate the distance between first co-ordinate with the second, second with the third, third with the fourth co-ordinates with the distances cumulatively being added up. The formula to calculate the distance remains the same.

In the code given, $i2 is iterated until the end of $#points, but I want it to stop after $i1+1. How do I modify the for loops such that when $i1 is 0, $i2 is 1 and $i1 is 2 with $i2 being 3 and so on. Or my logic is incorrect?
Thanks you very much for your time!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives