简体   繁体   English

在Ruby哈希上进行迭代,同时将值与另一个Ruby哈希进行比较

[英]Iterating over Ruby hash while comparing values to another Ruby hash

I have 2 Ruby objects that I am converting to hashes: one from XML and another from JSON. 我有2个要转换为哈希的Ruby对象:一个来自XML,另一个来自JSON。 When I puts the variable name I get hash, so it appears that I'm doing that correctly. 当我puts变量名时,我会得到哈希,因此看来我做得正确。

The format is several records in the format below. 格式是以下格式的几条记录。

Format of hash one ( smithjj being a unique username): 哈希一的格式( smithjj是唯一的用户名):

{ smithjj => {office => 331, buidling => 1} }

Format of hash 2: 哈希2的格式:

{"Data"=>{"xmlns:dmd"=>"http://www.xyz.com/schema/data-metadata",
"dmd:date"=>"2012-03-06", "Record"=>{"PCI"=>{"DPHONE3"=>nil, "OPHONE3"=>"111",
"DTY_DOB"=>"1956", "TEACHING_INTERESTS"=>nil, "FAX1"=>"123", "ROOMNUM"=>"111",
"DTD_DOB"=>"5", "DTM_DOB"=>"11", "WEBSITE"=>"www.test.edu", "FAX2"=>"324", 
"ENDPOS"=>"Director", "LNAME"=>"Smith", "FAX3"=>"4891", "MNAME"=>"Thomas",
"GENDER"=>"Male", "ALT_NAME"=>nil, "PFNAME"=>"TG", "id"=>"14101823488", 
"RESEARCH_INTERESTS"=>nil, "BIO"=>"", "CITIZEN"=>"Yes", "EMAIL"=>"test@email", 
"SUFFIX"=>nil, "DPHONE1"=>nil}, "termId"=>"234", "IndexEntry"=>{"text"=>"Other", 
"indexKey"=>"DEPARTMENT", "entryKey"=>"Other"}, "dmd:surveyId"=>"23424", 
"username"=>"smithers", "userId"=>"23324"}, "xmlns"=>"http://www.adsfda.com/"}}

I want to iterate over each unique username in the first hash and compare values from the PCI section of the second hash to the values in the first hash. 我想遍历第一个哈希中的每个唯一用户名,并将第二个哈希的PCI部分中的值与第一个哈希中的值进行比较。 The keys are different names so I planned on pairing them up. 键是不同的名称,所以我计划将它们配对。

I've tried several ways of doing it, but I keep getting a string integer error, so I must not be iterating correctly. 我已经尝试了几种方法来实现,但是我一直收到字符串整数错误,因此我不能正确地进行迭代。 I'm doing an .each do block, but all the examples I see show a simple hash, not a key => key => value, key => value . 我正在执行.each do块,但是我看到的所有示例都显示了一个简单的哈希,而不是key => key => value, key => value

Any direction is much appreciated. 任何方向都非常感谢。

Here's how you should be able to do the iteration properly for hashes, if you're not already using this: 如果您尚未使用此方法,则可以通过以下方法正确地进行哈希处理:

h = {:foo => 42, :bar => 43, 44 => :baz}
h.each {|key, val| puts "The value at #{key} is #{val}."}

This produces: 这将产生:

The value at foo is 42.
The value at bar is 43.
The value at 44 is baz.

As for the last part of your question, could you please make it a little clearer? 至于问题的最后一部分,请您说清楚一点吗? eg, what error are you getting exactly? 例如,您究竟得到什么错误? This could help us understand the issue more. 这可以帮助我们更多地了解该问题。

EDIT: If what you'd like to do is compare certain values against others, try something like this: 编辑:如果您想做的是将某些值与其他值进行比较,请尝试如下操作:

h1 = {:foo => 42, :bar => 43, 44 => :baz, :not_used => nil}
h2 = {:life_universe => 42, :fourty_three => 45, :another_num => 44, :unused => :pancakes}
# Very easy to add more
comparisons = {:foo => :life_universe, :bar => :fourty_three, :baz => :another_num}
def all_match_up?(hash1, hash2, comps)
  comparisons.each {|key, val|
    if key != val
      # They don't match!
      puts "#{key} doesnt match #{val}!"
      return false
    end
  }
  # They all match!
  true
end
all_match_up?(h1, h2, comparisons)

Try this to see what happens ;) 试试这个看看会发生什么;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM