这是我的数据
nodes x y z
1112, 23, 56, 88
2223, 56, 78, 54
3345, 32, 12, 11
4321, 11, 10, 06
5234, 10, 10, 12
6123, 12, 06, 04
现在我想要那些彼此最近的节点..例如:如果节点(1112)和节点(4321)之间的距离小于(让d = 10)节点(1112)和节点(3345)之间的距离(让d=34),反之亦然,输出应为=
node:1112 and node:4321 are closest
node:6123 and node:3345 are closest.....
我正在使用这样的距离公式: my $dist = sqrt( ($x-$x2)* 2 + ($y-$y2) *2 + ($z-$z2)**2 );
i.e:
d=sqrt( (56-23)**2 + (78-56)**2 + (54-88)**2 )
d=sqrt( (32-23)**2 + (12-56)**2 + (11-88)**2 )
d=sqrt( (11-23)**2 + (10-56)**2 + (06-88)**2 )
d=sqrt( (10-23)**2 + (10-56)**2 + (12-88)**2 )....and so on
对于其他节点及其坐标类似
d=sqrt( (23-56)**2 + (56-78)**2 + (88-54)**2 )
d=sqrt( (32-56)**2 + (12-78)**2 + (11-54)**2 )
d=sqrt( (11-56)**2 + (10-78)**2 + (06-54)**2 )
d=sqrt( (10-56)**2 + (10-78)**2 + (12-54)**2 )....
我已经这样做了,但一无所获......
use strict;
use warnings;
use Data::Dumper;
open(IN , "<" , "1.txt");
#open(OUT , ">" , "out.txt");
my $node_flag=0;
my %node_hash_1;
my %node_hash_2;
my @node_1;
my @node_2;
my @distance_array;
my $i=0;
foreach my $line(<IN>)
{
if($line=~/^\*node$/i)
{
$node_flag=1;
next;
}
if($node_flag==1)
{
if($line!~/^\*\*/i)
{
my @array=split(",",$line); # or my @array=(split(",",$line))[1,2,3]
my $node_id=shift @array;
$node_hash_1{$node_id}=\@array;
push(@node_1,[@array]);
push(@node_2,[@array]);
foreach my $var1(@node_1)
{
my($x,$y,$z)=@{$var1};
foreach my $var2(@node_2)
{
my($x2,$y2,$z2)=@{$var2};
my $dist = sqrt( ($x-$x2)**2 + ($y-$y2)**2 + ($z-$z2)**2 );
# print $dist;
print"\n";
foreach my $value(@{$node_hash_1{$node_id}})
{
my $vars=join",",@{$node_hash_1{$node_id}};
my @arr=split ",",$vars;
# print $arr[0];
print $vars;
if ($x==$value && $y==$value && $z==$value)
{
push @{$node_hash_1{$node_id}},$dist;
# $node_hash_2{x} = $x;
# $node_hash_2{y} = $y;
# $node_hash_2{z} = $z;
}
}
}
}
}
else
{
$node_flag=0;
}
}
}
#print Dumper(\%node_hash);
#print Dumper(\@distance_array);
#print Dumper(\@node_1);
#print Dumper(\%node_hash_2);