macoughl
macoughl

Reputation: 611

Combine 4 arrays to create a hash within a hash

I have the following arrays

campaign_names = ["Air Duct Cleaning", "Carpet Cleaning", "Brand Terms", "Upholstery Cleaning"]

impressions = ["358", "1404", "1853", "184"]

clicks = ["3", "6", "53", "0"]

conversions = ["0", "0", "2", "0"]

I'd like to create a hash within a hash so that the campaign_names elements are the nested hashes then impressions, clicks, and conversions are the keys within and the values are the array elements of each. The desired output is -

Hash = {:Air_Duct_Cleaning => { :impressions => "358", :clicks => "3", :conversions => "0" }, :Carpet_Cleaning => { :impressions => "1404", :clicks => "6", :conversions => "0" }, :Brand_Terms => { :impressions => "1853", :clicks => "53", :conversions => "2" }, :Upholstery_Cleaning => { :impressions => "184", :clicks => "0", :conversions => "0" }}

I've been trying to use #zip to combine them and then I was hoping to use Hash[] but end up with an empty hash (seems like I can only do that with 2 arrays)

zipped = campaign_names.zip(impressions, clicks, conversions)
=> [["Air Duct Cleaning", "358", "3", "0"], ["Carpet Cleaning", "1404", "6", "0"], ["Brand Terms", "1853", "53", "2"], ["Upholstery Cleaning", "184", "0", "0"]]
final_hash = Hash[zipped]
=> {}

I then tried using #each_with_index but the output I get is just the array I'm calling it on

hash = {}
campaign_names.each_with_index { |key, index| hash[key] = values[index] }
=> ["Air Duct Cleaning", "Carpet Cleaning", "Brand Terms", "Upholstery Cleaning"]

Upvotes: 0

Views: 252

Answers (5)

TalkativeTree
TalkativeTree

Reputation: 619

Just to be different and not use an each_with:

hash = {}
campaign_names.length.times do |index| 
    hash[campaign_names[index].gsub(/\s/, '_')] = {
      impressions: impressions[index],
      clicks: clicks[index], 
      conversions: conversions[index]
    } 
end

> hash
=> {"Air_Duct_Cleaning"=>{:impressions=>"358", :clicks=>"3", :conversions=>"0"},
    "Carpet_Cleaning"=>{:impressions=>"1404", :clicks=>"6", :conversions=>"0"},
    "Brand_Terms"=>{:impressions=>"1853", :clicks=>"53", :conversions=>"2"},
    "Upholstery_Cleaning"=>{:impressions=>"184", :clicks=>"0", :conversions=>"0"}}

Upvotes: 0

Vitalyp
Vitalyp

Reputation: 1089

Just for fun:

campaign_names.zip(1..campaign_names.size)
  .map{ |x,y| 
        { x => { impressions: impressions[y-1], 
                 clicks: clicks [y-1], 
                 conversions: conversions[y-1] 
               } 
        }
  }.inject("merge")

Upvotes: 1

JordanD
JordanD

Reputation: 163

hash = {}
campaign_names.each_with_index { |e,i|
    hash[e.gsub(' ','_').to_sym] = {impressions: impressions[i], clicks: clicks[i], conversations: conversions[i]}
}

Upvotes: 1

dano
dano

Reputation: 1

I think you're pretty close with you each_with_index

This worked for me...

hash = {}
campaign_names.each_with_index { |c,index| hash[c] = { impressions: impressions[index],      clicks: clicks[index], conversions: conversions[index] }}

Note this returns the campaign_names array but you should find hash will be populated as you require.

Upvotes: 0

sawa
sawa

Reputation: 168071

campaign_names.zip(impressions, clicks, conversions)
.map{|k, v1, v2, v3| [
  k.gsub(" ", "_").to_sym,
  {impressions: v1, clicks: v2, conversions: v3}
]}.to_h

Result:

{
  :Air_Duct_Cleaning   => {
    :impressions => "358",
    :clicks      => "3",
    :conversions => "0"
  },
  :Carpet_Cleaning     => {
    :impressions => "1404",
    :clicks      => "6",
    :conversions => "0"
  },
  :Brand_Terms         => {
    :impressions => "1853",
    :clicks      => "53",
    :conversions => "2"
  },
  :Upholstery_Cleaning => {
    :impressions => "184",
    :clicks      => "0",
    :conversions => "0"
  }
}

Upvotes: 1

Related Questions