So, I'm trying to compute the distance between 2 points in an XYZ format. I'm not quite sure if I'm calling the distance wrong, or if my formula is messed up, or all of the above and anything else. My input file looks like this:
1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
So far, this is what I have.
#!/usr/bin/perl use strict; use warnings; use diagnostics; my $source = "./CONTCAR"; my $destination = "./OUTPUT"; open(IN, '<', $source) or die "Couldn't open $source: $!\n"; open(OUT, '>', $destination) or die "Couldn't write to $destinatio +n: $!\n"; my @data = map [ split ], grep /\S/, <IN>; foreach (@data) { print "$_\n"; } for my $i (0 .. $#data) { for my $j (0 .. $#data) { next if $i == $j; my @coords_i = @{$data[$i]}[0,1,2]; my @coords_j = @{$data[$j]}[0,1,2]; printf OUT "%s to %s Distance=%.5f\n", $data[$i][0], $data[$j][0], $data[$i][2], $data[$j][2], distance(\@coords_i, \@coords_j); } } sub distance { my ($aa, $bb) = @_; my ($x, $y, $z) = map { $aa->[$_] - $bb->[$_] } 0 .. $#$aa; return sqrt(($x - $x)**2 + ($y - $y)**2 + ($z - $z)**2); } close IN; close OUT; print "Done.\n";
In the output file, this :
1 to 2 Distance=1.00000 1 to 3 Distance=1.00000 1 to 4 Distance=1.00000 1 to 5 Distance=1.00000 2 to 1 Distance=2.00000 2 to 3 Distance=2.00000 2 to 4 Distance=2.00000 2 to 5 Distance=2.00000 3 to 1 Distance=3.00000 3 to 2 Distance=3.00000 3 to 4 Distance=3.00000 3 to 5 Distance=3.00000 4 to 1 Distance=4.00000 4 to 2 Distance=4.00000 4 to 3 Distance=4.00000 4 to 5 Distance=4.00000 5 to 1 Distance=5.00000 5 to 2 Distance=5.00000 5 to 3 Distance=5.00000 5 to 4 Distance=5.00000
is printed. Any help is appreciated.

In reply to Subroutine not correct (I think) by jcklasseter

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.