Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple sub-hashes out of one hash

Tags:

regex

ruby

hash

I have a hash:

hash = {"a_1_a" => "1", "a_1_b" => "2", "a_1_c" => "3", "a_2_a" => "3",
        "a_2_b" => "4", "a_2_c" => "4"}

What's the best way to get the following sub-hashes:

[{"a_1_a" => "1", "a_1_b" => "2", "a_1_c" => "3"},
 {"a_2_a" => "3", "a_2_b" => "4", "a_2_c" => "4"}]

I want them grouped by the key, based on the regexp /^a_(\d+)/. I'll have 50+ key/value pairs in the original hash, so something dynamic would work best, if anyone has any suggestions.

like image 396
P.F. Avatar asked Dec 08 '16 20:12

P.F.


People also ask

Can a hash have multiple values?

You can only store scalar values in a hash. References, however, are scalars. This solves the problem of storing multiple values for one key by making $hash{$key} a reference to an array containing values for $key .

How do I access nested hash?

Accessing a specific element in a nested hash is very similar to a nested array. It is as simple as calling hash[:x][:y] , where :x is the key of the hash and :y is the key of the nested hash.

How do I create an array of hashes in Perl?

To add another hash to an array, we first initialize the array with our data. Then, we use push to push the new hash to the array. The new hash should have all of its data. As shown below, you can see the difference between the two arrays before and after pushing a new hash.

How do I check if a Perl hash is empty?

From perldoc perldata: If you evaluate a hash in scalar context, it returns false if the hash is empty. If there are any key/value pairs, it returns true; more precisely, the value returned is a string consisting of the number of used buckets and the number of allocated buckets, separated by a slash.


1 Answers

If you're only concerned about the middle component you can use group_by to get you most of the way there:

hash.group_by do |k,v|
  k.split('_')[1]
end.values.map do |list|
  Hash[list]
end

# => [{"a_1_a"=>"1", "a_1_b"=>"2", "a_1_c"=>"3"}, {"a_2_a"=>"3", "a_2_b"=>"4", "a_2_c"=>"4"}]

The final step is extracting the grouped lists and combining those back into the required hashes.

like image 82
tadman Avatar answered Sep 21 '22 10:09

tadman