1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
####
#!/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/, ;
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";
####
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