hash-of-hashes

how to make a deep_slice in a hash on ruby

喜你入骨 提交于 2021-02-08 03:26:52
问题 I was looking around for a clean way to do this and I found some workarounds but did not find anything like the slice (some people recommended to use a gem but I think is not needed for this operations, pls correct me if I am wrong), so I found myself with a hash that contains a bunch of hashes and I wanted a way to perform the Slice operation over this hash and get also the key/value pairs from nested hashes, so the question: Is there something like deep_slice in ruby? Example: input: a = {b

how to make a deep_slice in a hash on ruby

浪子不回头ぞ 提交于 2021-02-08 03:26:50
问题 I was looking around for a clean way to do this and I found some workarounds but did not find anything like the slice (some people recommended to use a gem but I think is not needed for this operations, pls correct me if I am wrong), so I found myself with a hash that contains a bunch of hashes and I wanted a way to perform the Slice operation over this hash and get also the key/value pairs from nested hashes, so the question: Is there something like deep_slice in ruby? Example: input: a = {b

Perl:Access values of hash inside a hash

試著忘記壹切 提交于 2020-01-02 09:59:31
问题 I have just picked up Perl. I have a little confusion with accessing hash values. Below is the code where I am trying to access the values of a hash inside a hash. Since am using a simple text editor to code, I am not able to figure out what can be the problem. Please help my %box = ( Milk => { A => 5, B => 10, C => 20, }, Chocolate => { AB => 10, BC => 25, CD => 40, }, ); foreach my $box_key(keys %box) { foreach my $inside_key (keys %box{box_key}) print "$box_key"."_$inside_key""is for

How to create a hash of hashes in C++?

白昼怎懂夜的黑 提交于 2020-01-02 09:58:18
问题 Is there a way to create a hash of hashes in C++? Effectively I am trying to do what you can do in Perl but only in C++. Here is an example of Perl code I would like to have happen in C++ %hash = ( gameobject1 => { position => { x_loc => 43, y_loc => 59, } rect_size => { width => 5, height => 3, } collidable => 1, sounds => { attack => "player_attack.ogg", jump => "player_jump1.ogg", jump_random => [qw/player_jump1.ogg player_jump2.ogg player_jump3.ogg/] } }, gameobject2 => { position => { x

Accessing elements of nested hashes in ruby [duplicate]

时光怂恿深爱的人放手 提交于 2019-12-27 20:11:16
问题 This question already has answers here : Ruby Style: How to check whether a nested hash element exists (16 answers) How to avoid NoMethodError for nil elements when accessing nested hashes? [duplicate] (4 answers) Closed 3 years ago . I'm working a little utility written in ruby that makes extensive use of nested hashes. Currently, I'm checking access to nested hash elements as follows: structure = { :a => { :b => 'foo' }} # I want structure[:a][:b] value = nil if structure.has_key?(:a) &&

Sorting by value Hash of Hashes Perl

↘锁芯ラ 提交于 2019-12-24 14:23:53
问题 Let's say I have a hash of hashes data structure constructed as followed: %HoH => ( flintstones => { family_members => "fred;wilma;pebbles;dino", number_of_members => 4, }, jetsons => { family_members => "george;jane;elroy", number_of_members => 3, }, simpsons => { family_members => "homer;marge;bart;lisa;maggie", number_of_members => 5, }, ) How do I sort the keys, the families in this case, by the value number_of_members from greatest to least? Then I would like to print out the highest two

Accessing values of json structure in perl

a 夏天 提交于 2019-12-18 17:11:54
问题 I have a json structure that I'm decoding that looks like this: person => { city => "Chicago", id => 123, name => "Joe Smith", pets => { cats => [ { age => 6, name => "cat1", type => "siamese", weight => "10 kilos" }, { age => 10, name => "cat2", type => "siamese", weight => "13 kilos" }, ], dogs => [ { age => 7, name => "dog1", type => "siamese", weight => "20 kilos" }, { age => 5, name => "dog2", type => "siamese", weight => "15 kilos" }, ], }, }, } I'm able to print the city , id , name by

Hashes of Hashes Idiom in Ruby?

≯℡__Kan透↙ 提交于 2019-12-17 21:47:03
问题 Creating hashes of hashes in Ruby allows for convenient two (or more) dimensional lookups. However, when inserting one must always check if the first index already exists in the hash. For example: h = Hash.new h['x'] = Hash.new if not h.key?('x') h['x']['y'] = value_to_insert It would be preferable to do the following where the new Hash is created automatically: h = Hash.new h['x']['y'] = value_to_insert Similarly, when looking up a value where the first index doesn't already exist, it would

Ruby dup/clone recursively

試著忘記壹切 提交于 2019-12-17 19:07:04
问题 I have a hash like: h = {'name' => 'sayuj', 'age' => 22, 'project' => {'project_name' => 'abc', 'duration' => 'prq'}} I need a dup of this hash, the change should not affect the original hash. When I try, d = h.dup # or d = h.clone d['name'] = 'sayuj1' d['project']['duration'] = 'xyz' p d #=> {"name"=>"sayuj1", "project"=>{"duration"=>"xyz", "project_name"=>"abc"}, "age"=>22} p h #=> {"name"=>"sayuj", "project"=>{"duration"=>"xyz", "project_name"=>"abc"}, "age"=>22} Here you can see the

How to assign hash['a']['b']= 'c' if hash['a'] doesn't exist?

做~自己de王妃 提交于 2019-12-17 06:38:10
问题 Is there any way simpler than if hash.key?('a') hash['a']['b'] = 'c' else hash['a'] = {} hash['a']['b'] = 'c' end 回答1: The easiest way is to construct your Hash with a block argument: hash = Hash.new { |h, k| h[k] = { } } hash['a']['b'] = 1 hash['a']['c'] = 1 hash['b']['c'] = 1 puts hash.inspect # "{"a"=>{"b"=>1, "c"=>1}, "b"=>{"c"=>1}}" This form for new creates a new empty Hash as the default value. You don't want this: hash = Hash.new({ }) as that will use the exact same hash for all