How to insert values in dynamically nested hash? - ruby

I have an array of strings of unknown length (but let's say up to 5). I have also an empty hash h = {} and a value.
I want to transform the array and the value to hash like this:
val = 1
h = {}
a = ['a', 'b', 'c', 'd']
# result I want:
{
'a' => {
'b' => {
'c' => {
'd' => 1
}
}
}
}
What's important is that some of the keys might already exist (created in a loop iteration before). So I might have:
val = 2
h = {'a' => {'b' => {'c' => {'d' => 1}}}}
a = ['a', 'b', 'c', 'e']
# result I want:
{
'a' => {
'b' => {
'c' => {
'd' => 1,
'e' => 2
}
}
}
}
Any ideas on how to do that?

Once again, inject to the rescue:
def dredge(hash, list, value = nil)
# Snip off the last element
*list, tail = list
# Iterate through the elements in the path...
final = list.inject(hash) do |h, k|
# ...and populate with a hash if necessary.
h[k] ||= { }
end
# Add on the final value
final[tail] = value
hash
end
This could be improved a little depending on how resilient you need it to be on things like zero-length lists and so on.
Here it is applied:
h = {}
a = ['a', 'b', 'c', 'd']
dredge(h, a, 1)
# => {"a"=>{"b"=>{"c"=>{"d"=>1}}}}
h = {'a' => {'b' => {'c' => {'d' => 1}}}}
a = ['a', 'b', 'c', 'e']
dredge(h, a, 2)
# => {"a"=>{"b"=>{"c"=>{"d"=>1, "e"=>2}}}}

Related

Converting a multi-level hash into a single hash

I have a source hash:
a = {'1' => 'A',
'2' => 'B',
'3' => 'C',
'4' => { '5' => 'D', '6' => 'E', '7' => { '8' => 'F', '9' => 'G' }},
'10' => {'11' => 'H'}}
I need to construct a method to make it a flat hash (single hash). The result should look like:
a = {'1' => 'A', '2' => 'B', '3' => 'C', '5' => 'D', '6' => 'E', '8' => 'F', '9' => 'G', '11' => 'H'}
I tried with merge, deep_merge, each_with_object, and recursion, but they did not give proper results.
Appreciate any help.
a = {'1' => 'A',
'2' => 'B',
'3' => 'C',
'4' => { '5' => 'D', '6' => 'E', '7' => { '8' => 'F', '9' => 'G' }},
'10' => {'11' => 'H'}}
# recursive detect value is Hash or not
compact_hash = ->(hh, h0={}) {
hh.reduce(h0) do |h, (k, v)|
if v.is_a? Hash
compact_hash[v, h]
else
h[k] = v
h
end
end
}
puts compact_hash[a]
Many thanks to #cary, I knew how to do one-line without ; and remove h.
compact_hash = ->(hh, h0={}) {
hh.each_with_object(h0) { |(k, v), h | v.is_a?(Hash) ? compact_hash[v, h] : h[k] = v }
}
h = { '1' => 'A', '2' => 'B', '3' => 'C',
'4' => { '5' => 'D', '6' => 'E', '7' => { '8' => 'F', '9' => 'G' } },
'10' => { '11' => 'H'} }
hh = h.dup
loop do
g = hh.select { |_,v| v.is_a? Hash }
break hh if g.empty?
g.keys.each { |k| hh.delete(k) }
g.values.each { |f| hh.update(f) }
end
#=> {"1"=>"A", "2"=>"B", "3"=>"C", "5"=>"D", "6"=>"E", "11"=>"H", "8"=>"F", "9"=>"G"}
This does not mutate h:
h #=> { "1"=>"A", "2"=>"B", "3"=>"C",
# "4"=>{"5"=>"D", "6"=>"E", "7"=>{"8"=>"F", "9"=>"G"}},
# "10"=>{"11"=>"H"}}
The antepenultimate line could be replaced by the following.
g_keys = g.keys
hh.delete_if { |k| g_keys.include?(k) }
I don't know which would be the more efficient.
a = {'1' => 'A',
'2' => 'B',
'3' => 'C',
'4' => { '5' => 'D', '6' => 'E', '7' => { '8' => 'F', '9' => 'G' }},
'10' => {'11' => 'H'}}
def flatten_hash(hash)
hash.each_pair.reduce({}) do |h, (k, v)|
v.is_a?(Hash) ? h.merge(flatten_hash(v)) : h.merge(k => v)
end
end
# pry(main)> flatten_hash(a)
#=> {"1"=>"A", "2"=>"B", "3"=>"C", "5"=>"D", "6"=>"E", "8"=>"F", "9"=>"G", "11"=>"H"}

Combine hash keys and values if keys are identical except for case

Let's say I have a Ruby Hash where at least two keys are identical, except for their case, for instance:
{ 'Foo' => 1, 'foo' => 2, 'bar' => 3 }
Is there a way I can combine like keys (except for their case) such that the resulting Hash might look like this?
{ 'foo' => 3, 'bar' => 3 }
Thank you!
You can build a new hash:
new_hash = Hash.new(0)
old_hash.each_pair { |k, v| new_hash[k.downcase] += v }
You can use inject to loop all the hash items and build a new hash.
hash = { 'Foo' => 1, 'foo' => 2, 'bar' => 3 }
hash.inject({}) do |result, (key, value)|
key = key.downcase
result[key] = result[key] ? result[key] + value : value
result
end
Here is one more way of doing this
h = { 'Foo' => 1, 'foo' => 2, 'bar' => 3 }
p h.collect{|k, v| {k.downcase => v}}.reduce { |a, v| a.update(v) {|k, o, n| o + n } }
#=> {"foo"=>3, "bar"=>3}
h = { 'Foo' => 1, 'foo' => 2, 'bar' => 3 }
h.each_with_object({}) { |(k,v),g| g[k.downcase] = g[k.downcase].to_i + v }
#=> {"foo"=>3, "bar"=>3}
This makes use of the fact that if g does not have a key e, g[e] on the right side will equal nil and nil.to_i #=> 0. On the other hand, if g has a key e, h[e].to_i will equal h[e].
Another way:
h.each_with_object({}) { |(k,v),g| g.update(k.downcase=>v) { |_,o,v| o+v } }
#=> {"foo"=>3, "bar"=>3}

ruby use array tvalues to index nested hash of hash [duplicate]

This question already has answers here:
What is the most ruby-ish way of accessing nested hash values at arbitrary depths? [duplicate]
(4 answers)
How to avoid NoMethodError for missing elements in nested hashes, without repeated nil checks?
(16 answers)
Closed 7 years ago.
In Ruby, I want to do something like this,
I have a hash of hash built like this.
h = {1 => {2 => {3 => "three"}},'a' => { 'b' => { 'c' => "basd"}}}
=> {"a"=>{"b"=>{"c"=>"basd"}}, 1=>{2=>{3=>"three"}}}
If I have an array with values like this.
a = [1, 2, 3]
I want to have a method which will use the array values to index nested keys in my hash and return the value pointed by last key (as guided by previous array/keys)
for eg.
getHashValue([1,2,3]) should return "three" => h[1][2][3]
if a = ['a','b', 'c'] then the return value should be basd.
How to get this done?
And then there's:
keys.inject(hash, :fetch)
or for earlier Ruby versions:
keys.inject(hash) {|h, k| h[k]}
If you did want to use recursion, a more Rubyesque approach would be:
def get_value(obj, keys)
keys.empty? ? obj : get_value(obj[keys[0]], keys[1..-1])
end
Simple recursion
def getValue(hash, keys, i = 0)
if i + 1 < keys.length
getValue(hash[keys[i]], keys, i + 1)
else
hash[keys[i]]
end
end
getValue(h, [1,2,3]) => "three"
getValue(h, ['a','b','c']) => "basd"
Ruby 2.3.0 introduced a new method called dig on both Hash and Array that solves this problem entirely.
It returns nil if an element is missing at any level of nesting.
h1 = {}
h2 = { a: {} }
h3 = { a: { b: {} } }
h4 = { a: { b: { c: 100 } } }
h1.dig(:a, :b, :c) # => nil
h2.dig(:a, :b, :c) # => nil
h3.dig(:a, :b, :c) # => nil
h4.dig(:a, :b, :c) # => 100
h = {1 => {2 => {3 => "three"}},'a' => { 'b' => { 'c' => "basd"}}}
a = ['a','b', 'c']
a.inject(h, :[]) # => "basd"
h = {1 => {2 => {3 => "three"}},'a' => { 'b' => { 'c' => "basd"}}}
a = [1, 2, 3]
a.inject(h, :[]) # => "three"

Search an Array of Hashes with a partial Hash in Ruby

Given a hash
z = [{'a' => 1, 'b' => 2}, {'a' => 3, 'b' => 4}, {'a' => 1, 'b' => 4}]
How do I search if the search parameter itself is a hash e.g.
{'a' => 3}
so that I can do something like z.find_by_hash({'a' => 3}) for it to return
{'a' => 3, 'b' => 4}
and also to get a collection of arrays like z.find_by_hash({'a' => 1}) for it to return
[{'a' => 1, 'b' => 2}, {'a' => 1, 'b => 4}]
Thanks
You can do this:
class Array
def find_by_hash(hash)
self.select { |h| h.includes_hash?(hash) }
end
end
class Hash
def includes_hash?(other)
included = true
other.each do |key, value|
included &= self[key] == other[key]
end
included
end
end
This extends Hash by a method to find out if a Hash includes another (with multiple keys and values). Array is extended with the method you wanted, but it's a more generic approach since you can do this:
ary = [ {:a => 1, :b => 3, :c => 5}, {:a => 5, :b => 2, :c => 8} ]
ary.find_by_hash( { :a => 1, :c => 5 } )
Note: You should also consider using Symbols for Hash keys since it is a common practice in Ruby, but my approach does also work with your keys.
z = [{'a' => 1, 'b' => 2}, {'a' => 3, 'b' => 4}, {'a' => 1, 'b' => 4}]
class Array
def search_hash(hash)
key = hash.keys.first
value = hash.values.first
select { |h| h[key] == value }
end
end
z.search_hash({'a' => 3}) #=> [{"a"=>3, "b"=>4}]
or you can type it without curly brackets
z.search_hash('a' => 3)
Basically what you need is something like this:
class Array
def find_by_hash(h)
h.collect_concat do |key, value|
self.select{|h| h[key] == value}
end
end
end
I didn't find an approach in API, so I think we have to implement it of our own.
(by the way, I think #megas' approach is better and more readable)
Code by TDD:
class SearchHashTest < Test::Unit::TestCase
def setup
#array_with_hash_elements = ArrayWithHashElements.new [{'a' => 1, 'b' => 2}, {'a' => 3, 'b' => 4}, {'a' => 1, 'b' => 4}]
end
def test_search_an_array_by_hash_parameter_and_return_single_hash
assert_equal( {'a' => 3, 'b' => 4}, #array_with_hash_elements.search({'a'=>3}) )
end
def test_search_an_array_by_hash_parameter_and_return_an_array
assert_equal( [{'a' => 1, 'b' => 2}, {'a'=> 1, 'b' => 4}], #array_with_hash_elements.search({'a'=>1}))
end
end
implemented code ( just for demo, not production)
class ArrayWithHashElements
def initialize some_array
#elements = some_array
end
def search( query_hash)
puts "search: #{query_hash.inspect}"
result = []
#elements.each do | array_element_in_hash_form|
query_hash.each_pair do | key, value |
if array_element_in_hash_form.has_key?(key) && array_element_in_hash_form[key] == value
puts "adding : #{array_element_in_hash_form.inspect} to result"
result << array_element_in_hash_form
end
end
result
end
return result.size != 1 ? result : result[0]
end
end
result:
sg552#siwei-moto:~/workspace/test$ ruby search_hash_test.rb
Loaded suite search_hash_test
Started
search: {"a"=>1}
adding : {"b"=>2, "a"=>1} to result
adding : {"b"=>4, "a"=>1} to result
.search: {"a"=>3}
adding : {"b"=>4, "a"=>3} to result
.
Finished in 0.000513 seconds.
2 tests, 2 assertions, 0 failures, 0 errors

Replacing values in a byte type NArray in Ruby

I am looking for a way to replace all occurrences of 'A' with 1, 'T' with 2, 'C' with 8, and 'G' with 16 in a byte array. How can this be done?
require "narray"
class NArray
def cast(type)
a = NArray.new(type,*self.shape)
a[] = self
a
end
end
conv = NArray.int(256)
atcg = NArray.to_na('ATCG', NArray::BYTE).cast(NArray::LINT)
conv[atcg] = [1,2,8,16]
seq_str = 'ABCDAGDE'
seq_ary = NArray.to_na(seq_str, NArray::BYTE).cast(NArray::LINT)
p conv[seq_ary]
#=> NArray.int(8):
# [ 1, 0, 8, 0, 1, 16, 0, 0 ]
Is it what you are looking for?
h = {'A' => 1, 'T' => 2, 'C' => 8, 'G' => 16}
a = ['A', 'B', 'C', 'D', 'A', 'G', 'D', 'E']
result = a.map {|c| h.include?(c) ? h[c] : c }

Resources