Questions tagged [hash-of-hashes]

A hash-of-hashes is a multi-dimensional hash that contains nested hashes.

A multi-dimensional hash is a flexible, nested structure where hash values are allowed to be other hashes. This data structure is natively supported by languages such as:

  • Perl
  • Python
  • Ruby

and may be simulated by others.

141 questions
46
votes
15 answers

Accessing elements of nested hashes in ruby

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…
Paul Morie
  • 15,528
  • 9
  • 52
  • 57
39
votes
7 answers

Converting nested hash keys from CamelCase to snake_case in Ruby

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…
Andrew Stewart
  • 710
  • 1
  • 6
  • 10
34
votes
2 answers

Hashes of Hashes Idiom in Ruby?

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…
David
  • 1,188
  • 12
  • 15
32
votes
16 answers

How to avoid NoMethodError for missing elements in nested hashes, without repeated nil checks?

I'm looking for a good way to avoid checking for nil at each level in deeply nested hashes. For example: name = params[:company][:owner][:name] if params[:company] && params[:company][:owner] && params[:company][:owner][:name] This requires three…
Kevin Sylvestre
  • 37,288
  • 33
  • 152
  • 232
27
votes
7 answers

Deleting a specific element from a nested hash

I am trying to work with a nested hash. I have a deck of cards represented as follows: deck_of_cards = { :hearts => {:two => 2, :three => 3, :four => 4, :five => 5, :six => 6, :seven => 7, :eight => 8, :nine => 9, :ten => 10, :jack => 10, …
BC00
  • 1,589
  • 3
  • 29
  • 47
24
votes
5 answers

Ruby dup/clone recursively

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 #…
Sayuj
  • 7,464
  • 13
  • 59
  • 76
21
votes
5 answers

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

Is there any way simpler than if hash.key?('a') hash['a']['b'] = 'c' else hash['a'] = {} hash['a']['b'] = 'c' end
Radek
  • 13,813
  • 52
  • 161
  • 255
17
votes
2 answers

Multiple keys pointing to a single value in Redis (Cache) with Java

I want to store multiple keys with a single value using jedis (Redis cache) with Java. I have three keys like user_1, driver_10, admin_5 and value = this is user, and I want to get value by using any one key among those three.
user3864113
  • 237
  • 1
  • 2
  • 8
13
votes
3 answers

how to create hash within hash

How am I able to create a hash within a hash, with the nested hash having a key to indentify it. Also the elements that I create in the nested hash, how can I have keys for them as well for example test = Hash.new() #create second hash with a…
paul
  • 133
  • 1
  • 1
  • 4
8
votes
4 answers

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

Given a hash such as: AppConfig = { 'service' => { 'key' => 'abcdefg', 'secret' => 'secret_abcdefg' }, 'other' => { 'service' => { 'key' => 'cred_abcdefg', 'secret' => 'cred_secret_abcdefg' } } } I need a…
JD.
  • 3,005
  • 2
  • 26
  • 37
7
votes
3 answers

Accessing values of json structure in perl

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…
bdizzle
  • 419
  • 2
  • 5
  • 19
6
votes
3 answers

How do I map (and sort) values from a hash of hashes?

I have a hash of hashes, like so: %hash = ( a => { b => 1, c =>2, d => 3}, a1 => { b => 11, c =>12, d => 13}, a2 => { b => 21, c =>22, d => 23} ) I want to extract the "b" element and put it into an array. Right now, I am…
sgsax
  • 393
  • 1
  • 4
  • 8
6
votes
4 answers

Perl hash of hashes of hashes of hashes... is there an 'easy' way to get an element at the end of the list?

I have a Perl hash of hashes of ... around 11 or 12 elements deep. Please forgive me for not repeating the structure below! Some of the levels have fixed labels, e.g. 'NAMES', 'AGES' or similar so accessing these levels are fine as I can use the…
dgBP
  • 1,681
  • 6
  • 27
  • 42
4
votes
3 answers

How to access an element deep in an array of arrays without getting 'undefined method' error

When trying to access an element deep in an array of arrays, what is the best way to avoid getting the error 'undefined method `[]' for nil:NilClass' if an element doesn't exist? For example I'm currently doing this, but it seems bad to me: if…
Michael Irwin
  • 3,119
  • 5
  • 24
  • 40
4
votes
2 answers

Combining 2+ 'deep' (multi-dimensional) hashes in perl

There is a question that explains exactly what I want here: how to merge 2 deep hashes in perl However, the answer there does not seem to work for me (suggestions of using the Merge module). I have two hashes like so: $VAR1 = { '57494'…
dgBP
  • 1,681
  • 6
  • 27
  • 42
1
2 3
9 10