chase dougherty
chase dougherty

Reputation: 131

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!

Upvotes: 1

Views: 493

Answers (2)

Mauricio Klein
Mauricio Klein

Reputation: 527

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.

Upvotes: 3

Toby Chin
Toby Chin

Reputation: 61

Looks like you actually want an array of hashes. So, your first array is going to be your list of keys for your hashes (I'll reference to this as "HEADER_KEYS"). In your second array, I see "\r\n". You may want to back up a step. I'm assuming this is coming from a CSV, so there are known delimiters and an unknown number of rows. To start parsing your CSV, split on the "\r\n" (or whatever the line break happens to be), and then iterate over each of those items and split on the commas. Something like:

final_dataset = []
HEADER_KEYS = [].freeze # put your actual array_of_headers here and freeze it
array_of_rows = []
csv_string.split("\r\n").each { |row| array_of_rows.push(row.split) }

This should give you an array of arrays that you can loop over.

array_of_rows.each do |row|
  row_hash = {}
  HEADER_KEYS.each_with_index do |key, index|
    row_hash[key] = row[index]
  end
  final_dataset.push(row_hash)
end

There may be a more elegant way of handling this, but this should do the trick to get you going.

Upvotes: 1

Related Questions