简体   繁体   中英

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. When I puts the variable name I get hash, so it appears that I'm doing that correctly.

The format is several records in the format below.

Format of hash one ( smithjj being a unique username):

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

Format of hash 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. 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 .

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 ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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