Update: Fixed loop variables properly.

OK, fixing it up. I've commented out the input/output file stuff, and just used <DATA> at the end for convenience:

#!/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 $destination: +$!\n"; my @data = map [ split ], grep /\S/, <DATA>; foreach (@data) { print "@$_\n"; } for my $i (0 .. $#data-1) { for my $j ($i+1 .. $#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", printf "(%s,%s) to (%s,%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**2 + $y**2 + $z**2); } # close IN; # close OUT; # print "Done.\n"; exit; __DATA__ 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5

Which outputs:

1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 (1,2) to (1,2) Distance=1.73205 (1,3) to (1,3) Distance=3.46410 (1,4) to (1,4) Distance=5.19615 (1,5) to (1,5) Distance=6.92820 (2,3) to (2,3) Distance=1.73205 (2,4) to (2,4) Distance=3.46410 (2,5) to (2,5) Distance=5.19615 (3,4) to (3,4) Distance=1.73205 (3,5) to (3,5) Distance=3.46410 (4,5) to (4,5) Distance=1.73205

-QM
--
Quantum Mechanics: The dreams stuff is made of


In reply to Re: Subroutine not correct (I think) by QM
in thread 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.