2012-08-02 92 views
-3

键的打印值我想在Perl比较哈希值,具有相同的价值观

11000, 67676, -7878, 9898 
11001, 67676, -7878, 7673 
11789, 56565, -0909, 5555 
17654, 67676, -7654, 3214 
18776, 99999, -55, 4444 
17765, 67676, 7878, 9898 

scan *nodes 
hash1{node}=x,y,z 
invert_y=y*-1 
chech invert_y existance in hash2 
if exists 
hash2{y}=[n1,n2,n3...].append the node 
else 
hash2{y}=store node in a array and pass its reference as value 


foreach key in hash1 
get x1,y1,z1 of this node1 (eg. hash1{key} will return x,y,z of this node1) 
invert_y=y*1 
if exists hash2{invert_y} 
get all node of hash2{keys} in node_array 
foreach node2 in node_array 
get x2,y2,z2 of this node2 (eg. hash1{node2} will return x,y,z of this node2) 
if x1 of node1 == x2 of node2 && z1 of node1 == z2 of node2 
    node1 and node2 are symmetric 
+0

因此,请继续并在Perl中实现它;但对我来说,你的speudo-code没有意义,所以这将是一种痛苦,我担心...... – pavel 2012-08-02 20:31:11

回答

2

我把你的一些伪代码,并执行该算法的perl-美化版它让你开始。

hash1{node}=x,y,z 
    # value in hash is a reference to a 3-element array 
    $hash1{$node} = [ $x, $y, $z ]; 

foreach key in hash1 
    foreach my $k (keys %hash1) 

get x1,y1,z1 of this node1 (eg. hash1{key} will return x,y,z of this node1) 
    $node1 = $hash1{$k}; 

invert_y=y*1 
    I have no idea what you mean here 

if exists hash2{invert_y} 
    if (exists $hash2{$invert_y} 

get all node of hash2{keys} in node_array 
    eh? 

foreach node2 in node_array 
    foreach $node2 (@node_array) 

get x2,y2,z2 of this node2 (eg. hash1{node2} will return x,y,z of this node2) 
    $node2 = $hash1{$node2}; 

if x1 of node1 == x2 of node2 && z1 of node1 == z2 of node2 
    node1 and node2 are symmetric 

    # x is element 0, y is 1 and z is 2 
    if ($node1->[0] == $node2->[0] && 
     $node1->[2] == $node2->[2])