I made two hashes hash1 and hash2 with following keys and values.
Input:
hash1 = {1=>"One", 2=>"Two", 3=>"Three"}
hash2 = {4=>"First", 5=>"Second", 6=>"Third"}
Result:
And I am expecting hash3 to give me a result like
hash3 #=> {1 => "One", 2 => "Two", 3 => "Three", 4 => "First", 5 => "Second", 6 => "Third"}
Can anyone tell me how I can merge these two hashes? Assume that hash3 should contain all keys and all values.
Solution with minimal coding
hash3 = hash1.merge(hash2)
It seems you are looking for Hash#merge
hash1 = {1=>"One", 2=>"Two", 3=>"Three"}
hash2 = {4=>"First", 5=>"Second", 6=>"Third"}
hash3 = hash1.merge hash2
#=> {1 => "One", 2 => "Two", 3 => "Three", 4 => "First", 5 => "Second", 6 => "Third"}
Considering the fact that you access your hash through an increasing sequence of integers, you should think about using an Array instead of a Hash.
result = hash1.values + hash2.values
# => ["One", "Two", "Three", "First", "Second", "Third"]
This way you can still access your values similarly (e.g. result[2] #=> "Three"), but you have a more concise data structure.
If you want 1 to return both "first" and "one", you'll need to store them in an array which will then be the value of which 1 maps to.
results = {}
hash1.each do |key, value|
if results[key].nil?
results[key] = [value]
else
results[key] << value
hash2.each do |key, value|
if results[key].nil?
results[key] = [value]
else
results[key] << value
results[1]
=> ["one", "first"]
Update
You obviously don't want a hash. You want an array with the values only.
results = hash1.values + hash2.values
=> ["one", "two", "three", "first", "second", "third"]
You can access this array just like a hash. Keep in mind that arrays are zero-indexed, so:
results[0]
=> "one"
results[2]
=> "three"
results[5]
=> "third"
Update 2
And now you want a hash after all, because you have unique keys. So:
hash3 = hash1.merge(hash2)
Since you're learning a new language and are still learning how things work. I'd suggest that with whatever you're working with you take a look at the developer docs.
http://ruby-doc.org/
From there you can search for whatever you're looking for and find other interesting things you can do with the Objects ruby provides for you.
To answer your question specifically you'll need merge method for hashes.
{1: 'One', 2: 'Two', 3: 'Three'}.merge(4: 'Four', 5: 'Five', 6: 'Six)
Will yield {1: 'One', 2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five', 6: 'Six'}
Related
In Ruby, what's an elegant way to combine 2 strings from an array without repeating them?
Example:
array = ['one', 'two', 'three', 'four']
And I want my output to be:
['onetwo', 'onethree', 'onefour', 'twothree', 'twofour', 'threefour']
Seems simple enough but I've been pretty stumped!
Edit: this is for doing something with anagrams so order does not matter. ('onetwo' ends up being equivalent to 'twoone'.)
You can use Arrary#combination, Enumerable#map and Array#join for that.
Code
array.combination(2).map(&:join)
#=> ["onetwo", "onethree", "onefour", "twothree", "twofour", "threefour"]
Explanation
array = ['one', 'two', 'three', 'four']
a = array.combination(2)
#=> #<Enumerator: ["one", "two", "three", "four"]:combination(2)>
To view the contents of the enumerator:
a.to_a
#=> [["one", "two" ], ["one", "three"], ["one" , "four"],
# ["two", "three"], ["two", "four" ], ["three", "four"]]
Then
a.map(&:join)
which has the same effect as:
a.map { |e| e.join }
#=> ["onetwo", "onethree", "onefour", "twothree", "twofour", "threefour"]
If you wanted "twoone" as well as "onetwo", use Array#permutation instead of combination:
array.permutation(2).map(&:join)
#=> ["onetwo" , "onethree", "onefour" , "twoone" , "twothree", "twofour",
# "threeone", "threetwo", "threefour", "fourone", "fourtwo" , "fourthree"]
This question already has answers here:
Remove redundant or duplicate tuples in Ruby array
(6 answers)
Closed 9 years ago.
So i have this array:
['One', 'Two', 'One', 'One', 'Three', 'Three', 'One']
I want to get this result:
['One', 'Two', 'One', 'Three', 'One']
So I want to remove items that is the same as previous item.
How to do that in Ruby?
Use Enumerable#chunk
ary = ['One', 'Two', 'One', 'One', 'Three', 'Three', 'One']
ary.chunk {|x| x }.map(&:first)
#=> ["One", "Two", "One", "Three", "One"]
Enumerable#chunk enumerates over the items, chunking them together based on the return value of the block. consecutive elements which return the same block value are chunked together.
ary.chunk {|x| x }.to_a
=> [["One", ["One"]],
["Two", ["Two"]],
["One", ["One", "One"]],
["Three", ["Three", "Three"]],
["One", ["One"]]]
map(&:first) Array#map iterates over the array calling the first method on each element of the array
&:first is called symbol to proc which creates a proc object that map yields each element of the array to
so the example above could be rewritten as below:
lambda_obj = ->(ele) { ele.first }
ary.chunk {|x| x }.map(&lambda_obj)
#=> ["One", "Two", "One", "Three", "One"]
Pretty straightforward with inject:
ary.inject [] do |accum,x|
accum.last == x ? accum : accum << x
end
I'm trying to combine two arrays into a hash.
#sample_array = ["one", "Two", "Three"]
#timesheet_id_array = ["96", "97", "98"]
I want to output the results into a hash called #hash_array. Is there a simple way to combine the two in a code block so that if you call puts at the end it looks like this in the console
{"one" => "96", "Two" => "97", "Three" => "98"}
I think this could be done in one or two lines of code.
try this
keys = [1, 2, 3]
values = ['a', 'b', 'c']
Hash[keys.zip(values)]
thanks
#hash_array = {}
#sample_array.each_with_index do |value, index|
#hash_array[value] = #timesheet_id_array[index]
end
Imho that looks best:
[:a,:b,:c].zip([1,2,3]).to_h
# {:a=>1, :b=>2, :c=>3}
Dr. Nic suggests 2 options explained well at http://drnicwilliams.com/2006/10/03/zip-vs-transpose/
#hash_array = {}
0.upto(#sample_array.length - 1) do |index|
#hash_array[#sample_array[index]] = #timesheet_id_array[index]
end
puts #hash_array.inspect
I've got an unsorted array of keys like this:
keys = ["ccc", "ddd", "ggg", "aaa", "bbb"]
and a hash
hash = {"ddd" => 4, "aaa" => 1, "bbb" => 2, "eee" => 5, "fff" => 6}
I'd like to join these two data structures to return a hash in the original order of keys to the first keys:
{"ccc" => nil, "ddd" => 4, "ggg" => nil, "aaa" => 1, "bbb" => 2}
Items NOT in the hash (like "ggg") should return nil.
This is analogous to the "v-lookup" function in excel.
this is in ruby. Thanks!
Cryptic:
Hash[keys.zip(hash.values_at *keys)]
Or a bit longer, a bit less cryptic:
keys.map.with_object({}) {|key, memo| memo[key] = hash[key]}
how can I do in Ruby an array with indexes?
My custom from PHP is something like this:
#my_array = [0 => "a", 3 => "bb", 7 => "ccc"]
And this array I want to go through each_with_index and I would like to get the result, e.g. in a shape:
0 - a
3 - bb
7 - ccc
Can anyone help me, how to do?
Thanks
They're called hashes in ruby.
h = { 0 => "a", 3 => "bb", 7 => "ccc" }
h.each {|key, value| puts "#{key} = #{value}" }
Reference with a bunch of examples here: Hash.
You don't want an array, you want to use a hash. Since your indices are not sequential (as they would/should be if using an array), use a hash like so:
#my_hash = { 0 => 'a', 3 => 'bb', 7 => 'ccc' }
Now you can iterate through it like this:
#my_hash.each do |key, value|
num = key
string = value
# do stuff
end
Arrays in ruby already have indexes but if you want an associative array with index of your choice, use a Hash:
#my_array = {0 => "a", 3 => "bb", 7 => "ccc"}