jcklasseter has asked for the wisdom of the Perl Monks concerning the following question:
So far, this is what I have.1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
In the output file, this :#!/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";
is printed. Any help is appreciated.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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Subroutine not correct (I think)
by Corion (Patriarch) on Jun 17, 2015 at 12:59 UTC | |
by jcklasseter (Sexton) on Jun 17, 2015 at 13:10 UTC | |
by hippo (Archbishop) on Jun 17, 2015 at 14:28 UTC | |
|
Re: Subroutine not correct (I think)
by jdporter (Paladin) on Jun 17, 2015 at 14:09 UTC | |
|
Re: Subroutine not correct (I think)
by pme (Monsignor) on Jun 17, 2015 at 13:08 UTC | |
by jcklasseter (Sexton) on Jun 17, 2015 at 13:11 UTC | |
by pme (Monsignor) on Jun 17, 2015 at 13:31 UTC | |
by jcklasseter (Sexton) on Jun 17, 2015 at 13:38 UTC | |
by roboticus (Chancellor) on Jun 17, 2015 at 13:43 UTC | |
by locked_user sundialsvc4 (Abbot) on Jun 17, 2015 at 13:18 UTC | |
by jcklasseter (Sexton) on Jun 17, 2015 at 13:25 UTC | |
|
Re: Subroutine not correct (I think)
by QM (Parson) on Jun 17, 2015 at 13:25 UTC | |
by jcklasseter (Sexton) on Jun 17, 2015 at 13:28 UTC | |
by Corion (Patriarch) on Jun 17, 2015 at 13:33 UTC | |
|
Re: Subroutine not correct (I think)
by QM (Parson) on Jun 17, 2015 at 14:00 UTC | |
|
Re: Subroutine not correct (I think)
by BillKSmith (Monsignor) on Jun 17, 2015 at 14:53 UTC | |
|
Re: Subroutine not correct (I think)
by jcklasseter (Sexton) on Jun 17, 2015 at 12:48 UTC |