hash-of-hashes

Creating hash of hash dynamically in perl

我与影子孤独终老i 提交于 2019-12-02 05:15:44
问题 I am trying to create a hash of hash of - the nesting depth depends on the number of arguments passed into @aGroupByFields array. In the below implementation, I am getting the desired hash structure.But I have hard coded the fields [ example - $phBugRecord->{createdBy} ] instead of deriving it from the array. I am not sure how to dynamically create this. my (@aGroupByFields) = ['createdBy','status','devPriority']; # In real case,these are passed in as arguments my (%hTemp); # This is the

Creating hash of hash dynamically in perl

一个人想着一个人 提交于 2019-12-02 02:37:26
I am trying to create a hash of hash of - the nesting depth depends on the number of arguments passed into @aGroupByFields array. In the below implementation, I am getting the desired hash structure.But I have hard coded the fields [ example - $phBugRecord->{createdBy} ] instead of deriving it from the array. I am not sure how to dynamically create this. my (@aGroupByFields) = ['createdBy','status','devPriority']; # In real case,these are passed in as arguments my (%hTemp); # This is the final hash which will be structured according to above fields # %hBugDetails is the hash containing details

What is the most ruby-ish way of accessing nested hash values at arbitrary depths? [duplicate]

不羁的心 提交于 2019-11-30 19:32:20
This question already has an answer here: How to avoid NoMethodError for missing elements in nested hashes, without repeated nil checks? 17 answers Given a hash such as: AppConfig = { 'service' => { 'key' => 'abcdefg', 'secret' => 'secret_abcdefg' }, 'other' => { 'service' => { 'key' => 'cred_abcdefg', 'secret' => 'cred_secret_abcdefg' } } } I need a function to return service/key in some cases and other/service/key in other cases. A straightforward way is to pass in the hash and an array of keys, like so: def val_for(hash, array_of_key_names) h = hash array_of_key_names.each { |k| h = h[k] }

Accessing values of json structure in perl

ぃ、小莉子 提交于 2019-11-30 16:04:14
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 doing: foreach my $listing ($decoded->{person}) { my $city = $listing->{city}; my $name = $listing->

Converting nested hash keys from CamelCase to snake_case in Ruby

大憨熊 提交于 2019-11-30 08:09:43
I'm trying to build an API wrapper gem, and having issues with converting hash keys to a more Rubyish format from the JSON the API returns. The JSON contains multiple layers of nesting, both Hashes and Arrays. What I want to do is to recursively convert all keys to snake_case for easier use. Here's what I've got so far: def convert_hash_keys(value) return value if (not value.is_a?(Array) and not value.is_a?(Hash)) result = value.inject({}) do |new, (key, value)| new[to_snake_case(key.to_s).to_sym] = convert_hash_keys(value) new end result end The above calls this method to convert strings to

What is the most ruby-ish way of accessing nested hash values at arbitrary depths? [duplicate]

百般思念 提交于 2019-11-30 03:39:51
问题 This question already has answers here : How to avoid NoMethodError for missing elements in nested hashes, without repeated nil checks? (17 answers) Closed 3 years ago . Given a hash such as: AppConfig = { 'service' => { 'key' => 'abcdefg', 'secret' => 'secret_abcdefg' }, 'other' => { 'service' => { 'key' => 'cred_abcdefg', 'secret' => 'cred_secret_abcdefg' } } } I need a function to return service/key in some cases and other/service/key in other cases. A straightforward way is to pass in the

Converting nested hash keys from CamelCase to snake_case in Ruby

情到浓时终转凉″ 提交于 2019-11-29 11:16:56
问题 I'm trying to build an API wrapper gem, and having issues with converting hash keys to a more Rubyish format from the JSON the API returns. The JSON contains multiple layers of nesting, both Hashes and Arrays. What I want to do is to recursively convert all keys to snake_case for easier use. Here's what I've got so far: def convert_hash_keys(value) return value if (not value.is_a?(Array) and not value.is_a?(Hash)) result = value.inject({}) do |new, (key, value)| new[to_snake_case(key.to_s).to

Hashes of Hashes Idiom in Ruby?

流过昼夜 提交于 2019-11-28 16:32: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 be preferable if nil is returned rather than receiving an undefined method for '[]' error. looked_up

Ruby dup/clone recursively

旧巷老猫 提交于 2019-11-28 09:40:50
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 project['duration'] is changed in the original hash because project is another hash object. I want the hash

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

倾然丶 夕夏残阳落幕 提交于 2019-11-27 01:02:02
Is there any way simpler than if hash.key?('a') hash['a']['b'] = 'c' else hash['a'] = {} hash['a']['b'] = 'c' end 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 default entries. Also, as Phrogz notes, you can make the auto-vivified hashes auto-vivify using default_proc :