【问题标题】:compare hashes,print values of keys having same values比较哈希,打印具有相同值的键的值
【发布时间】:2012-08-02 18:17:07
【问题描述】:

我想在 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 

【问题讨论】:

  • 所以继续在 Perl 中实现它;但对我来说,你的伪代码毫无意义,所以我担心这会很痛苦......

标签: perl hash get append


【解决方案1】:

我提取了你的一些伪代码并对其进行了 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])

【讨论】:

    猜你喜欢
    • 2021-11-15
    • 1970-01-01
    • 2016-03-11
    • 2023-03-17
    • 2012-07-31
    • 1970-01-01
    • 2018-11-13
    • 2011-10-04
    • 1970-01-01
    相关资源
    最近更新 更多