1

我希望比较两个哈希以确定哪些值可以匹配。例如:

hash1 = {
    "hash1_key_a" => 1,
    "hash1_key_b" => 2
}

hash2 = {
    "hash2_key_a" => 1,
    "hash2_key_b" => 3,
    "hash2_key_c" => 4
}

# The method here should display the matching keys & values,
# and then delete them from their hashes. For example:

puts "#{hash1_key_a};#{hash2_key_a};1" # 1 is representing the referral we should
                                       # put in for the value

谁能指导我正确的方向?

4

1 回答 1

0

哈希是否有不同的键?然后你可以做这样的事情:

hash1.merge(hash2).group_by{|k,v| v}.each{ |v,ks|
   puts "#{ ks.map(&:first)*';' };#{ v }"
}
于 2012-05-24T21:15:56.920 回答