How to count duplicates hash itens in Ruby
-
I need to count the duplicates, they need to be 100% identical to increase my count, but I can not use a nothing out of Ruby 1.8.5, this code will run inside a plugin in google sketchup
product_list = [ { "product" => 1, "x" => 200, "y" => 100, "z" => 18}, { "product" => 1, "x" => 200, "y" => 100, "z" => 18}, { "product" => 1, "x" => 300, "y" => 100, "z" => 18}, { "product" => 2, "x" => 300, "y" => 100, "z" => 18}, { "product" => 2, "x" => 100, "y" => 100, "z" => 18}, { "product" => 2, "x" => 100, "y" => 100, "z" => 18}, { "product" => 3, "x" => 100, "y" => 100, "z" => 18} ]; product_list_result = product_list.count_duplicate(); product_list_result = [ { "product" => 1, "x" => 200, "y" => 100, "z" => 18, "count" = 2}, { "product" => 1, "x" => 300, "y" => 100, "z" => 18, "count" = 1}, { "product" => 2, "x" => 300, "y" => 100, "z" => 18, "count" = 1}, { "product" => 2, "x" => 100, "y" => 100, "z" => 18, "count" = 2}, { "product" => 3, "x" => 100, "y" => 100, "z" => 18, "count" = 1} ];
-
Sorry but your multiple very similar posts are getting confusing...
You can edit a previous post !
Why worry about a method that exists in a newer version, but is not available in this one...
You need to make a method of your own that compares an array of hashes and then reports duplicates.
I suggest .to_a and .sort will help to get indices and == will compare the hashes_as_array so you can count them... -
Run this code in Ruby 1.8.5 ( Ruby Console in Sketchup 8 )
product_list.dup.group_by { |h| h }.each_value.map do |value| value.first.tap { |hash| hash['count'] = value.count } end Error; #<NoMethodError; undefined method `group_by' for #<Array;0x12599cd4>> (eval);8
Run this code in Ruby 1.8.7 ( irb in OSX Terminal )
[{"count"=>2, "z"=>18, "y"=>100, "x"=>100, "product"=>2}, {"count"=>1, "z"=>18, "y"=>100, "x"=>300, "product"=>1}, {"count"=>2, "z"=>18, "y"=>100, "x"=>200, "product"=>1}, {"count"=>1, "z"=>18, "y"=>100, "x"=>100, "product"=>3}, {"count"=>1, "z"=>18, "y"=>100, "x"=>300, "product"=>2}]
-
Run this code in Ruby 1.8.5 ( Ruby Console in Sketchup 8 )
h = Hash.new 0; product_list.each {|p| h[p] += 1}; product_list_result = h.keys.map{|k| k["count"] = h[k]; k};
Result:
[{"x"=>300, "y"=>100, "z"=>18, "product"=>1, "count"=>1}, {"x"=>200, "y"=>100, "z"=>18, "product"=>1, "count"=>1}, {"x"=>100, "y"=>100, "z"=>18, "product"=>2, "count"=>1}, {"x"=>300, "y"=>100, "z"=>18, "product"=>2, "count"=>1}, {"x"=>200, "y"=>100, "z"=>18, "product"=>1, "count"=>1}, {"x"=>100, "y"=>100, "z"=>18, "product"=>3, "count"=>1}, {"x"=>100, "y"=>100, "z"=>18, "product"=>2, "count"=>1}]
Run this code in Ruby 1.8.7 ( irb in OSX Terminal )
Result:[{"product"=>1, "z"=>18, "y"=>100, "x"=>200, "count"=>2}, {"product"=>3, "z"=>18, "y"=>100, "x"=>100, "count"=>1}, {"product"=>2, "z"=>18, "y"=>100, "x"=>100, "count"=>2}, {"product"=>1, "z"=>18, "y"=>100, "x"=>300, "count"=>1}, {"product"=>2, "z"=>18, "y"=>100, "x"=>300, "count"=>1}]
-
result = product_list.inject(Hash.new(0)) {|h, e| h[e] += 1; h} result.each {|k, v| puts "#{k} --> #{v}" {"product"=>1, "x"=>200, "y"=>100, "z"=>18} --> 2 {"product"=>1, "x"=>300, "y"=>100, "z"=>18} --> 1 {"product"=>2, "x"=>300, "y"=>100, "z"=>18} --> 1 {"product"=>2, "x"=>100, "y"=>100, "z"=>18} --> 2 {"product"=>3, "x"=>100, "y"=>100, "z"=>18} --> 1
-
# EXAMPLE - untested module Romuloigor module Product @@product = { ;count => {}, ;list => [ { "product" => 1, "x" => 200, "y" => 100, "z" => 18}, { "product" => 1, "x" => 200, "y" => 100, "z" => 18}, { "product" => 1, "x" => 300, "y" => 100, "z" => 18}, { "product" => 2, "x" => 300, "y" => 100, "z" => 18}, { "product" => 2, "x" => 100, "y" => 100, "z" => 18}, { "product" => 2, "x" => 100, "y" => 100, "z" => 18}, { "product" => 3, "x" => 100, "y" => 100, "z" => 18} ] } class << self def add(product) # @@product[;list] << product # end # add() def recount() # @@product[;count]= @@product[;list].inject(Hash.new(0)) {|h, e| h[e] += 1; h} # end # recount() def count(product) # total = @@product[;count][product] return ( total ? total ; 0 ) # end # count() def list() # list = "" @@product_list[;count].each {|k,v| list << "#{k.inspect} count = #{v}\n" } return list # end # list() end # proxy class end # module Product end # module Romuloigor
Advertisement