Search code examples
arraysrubyhashiterationenumerate

Iterating through two arrays to make a hash in Ruby


I have two arrays that need to be combined into a hash. However, one of the arrays will always be the key. So I need to go through a list of names, numbers, addresses, etc and give them all titles. An example would be:

Adjustor name => Chase, Firm name => Chase Bank

and then repeat for another location.

Adjustor name => Rob, Firm name => Walmart.  

Here is what I have so far:

Array_headers = ['Adjuster Name','Firm Name', 'Firm Number', 'Adjustment Type', 'Address 1', 'Address 2', 'City', 'State', 'Zip Code', 'Phone', 'Fax', 'Website', 'Comments', 'Latitude', 'Longitude', 'Manual LongLatCalc', 'LongLat Error']

Data_examples = ["AdjusterName", "FirmName", "FirmNumber", "AdjustmentType", "Address1", "Address2", "City", "State", "ZipCode", "Phone", "Fax", "WebSite", "Comments", "Latitude", "Longitude", "ManualLongLatCalc", "LongLatError", "chase", "chase bank", "260-239-1761", "property", "501 w", "200 s", "albion", "in", "46701", "555-555-5555", "c@gamil", "whatsupwhatups.com", "hahahah", "12.332", "12.222", "no", "none"]

CombiningArrays= Hash[Array_headers.zip data_examples]

p CombiningArrays

It should return the following:

{"Adjuster Name"=>"AdjusterName", "Firm Name"=>"FirmName", "Firm Number"=>"FirmNumber", "Adjustment Type"=>"AdjustmentType", "Address 1"=>"Address1", "Address 2"=>"Address2", "City"=>"City", "State"=>"State", "Zip Code"=>"ZipCode", "Phone"=>"Phone", "Fax"=>"Fax", "Website"=>"WebSite", "Comments"=>"Comments", "Latitude"=>"Latitude", "Longitude"=>"Longitude", "Manual LongLatCalc"=>"ManualLongLatCalc", "LongLat Error"=>"LongLatError", *"Adjuster Name"=>" \r\nchase", "Firm Name"=>"chase", "Firm Number"=>"260-239-1761", "Adjustment Type"=>"property", "Address 1"=>"501 w", "Address 2"=>"200 s", "City"=>"albion", "State"=>"in", "Zip Code"=>"46701", "Phone"=>"555-555-5555", "Fax"=>"c@gamil", "Website"=>"whatsupwhatups.com", "Comments"=>"hahahah", "Latitude"=>"12.332", "Longitude"=>"12.222", "Manual LongLatCalc"=>"no", "LongLat Error"=>"none"*}

It stops at "LongLat Error"=>"LongLatError" and everything that is italicized does not show up. How do I get it to continually loop through my other array?

I also tried the following code:

#Creating a method to go through arrays

def array_hash_converter headers, data
    hash = Hash.new

headers.each_with_index do |header, index|
    hash[header] = data[index]
  end
    puts hash
  end

i=0

while i < data.count do
    array_hash_converter Array_header, data
    i+=1
    end

Please Help!


Solution

  • I suggest to slice the values array on the keys array length, and then just map them into an array of hashes. For example:

    sliced_values = Data_examples.each_slice(Array_headers.length)
    result = sliced_values.map { |slice| Array_headers.zip(slice).to_h }
    

    You will never get a single hash as result, because you'll have collision on the keys and, then, only the last result will be returned, since it overwrites the previous ones. Remember that hash keys are unique in Ruby.