mr_muscle
mr_muscle

Reputation: 2910

Ruby 2.7 count array of hash key

I've prepared a method which parses a log file and returns a hash as a result:

  def parse
    @hash = Hash.new { |hash, key| hash[key] = [] }
    File.open(file_path).each do |line|
      customer, product = line.split
      @hash[customer] << product
    end
    @hash
  end

result:

=> {"customer_id$1"=>["product_id$184", "product_id$184", "product_id$94", "product_id$16", 
"product_id$184", "product_id$592"],
"customer_id$3"=>["product_id&16", "product_id&196", "product_id&196", "product_id&82"],
"customer_id$6"=>["product_id$26", "product_id$126", "product_id$26", "product_id$26"]}

How to count all (e.g. customer_id$1 = 6) and unique values (e.g. customer_id$1 = 4) for each customer if they are an array? I'll have this code in AWS lambda so I must use pure Ruby 2.7.

Upvotes: 0

Views: 171

Answers (2)

Cary Swoveland
Cary Swoveland

Reputation: 110755

If g is your hash:

h = g.transform_values do |v|
  { nbr_products: v.size, nbr_uniq_products: v.uniq.size }
end
  #=> {"customer_id$1"=>{:nbr_products=>6, :nbr_uniq_products=>4},
  #    "customer_id$3"=>{:nbr_products=>4, :nbr_uniq_products=>3},
  #    "customer_id$6"=>{:nbr_products=>4, :nbr_uniq_products=>2}}

so, for example,

h["customer_id$1"][:nbr_products]
  #=> 6 
h["customer_id$6"][:nbr_uniq_products]
  #=> 2

See Enumerable#transform_values.

Upvotes: 2

Darkisa
Darkisa

Reputation: 2047

First lets put the result into a variable:

result = {"customer_id$1"=>["product_id$184", "product_id$184", "product_id$94", "product_id$16", 
"product_id$184", "product_id$592"],
"customer_id$3"=>["product_id&16", "product_id&196", "product_id&196", "product_id&82"],
"customer_id$6"=>["product_id$26", "product_id$126", "product_id$26", "product_id$26"]}

Then, we can loop each customer and get the total count and total unique count:

result.each |key, value|
  # total count
  value.size
  # total unique count
  value.uniq.size
end

Upvotes: 0

Related Questions