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
4
votes
5 answers

How can I create a hash of hashes in Perl?

I am new to Perl. I need to define a data structure in Perl that looks like this: city 1 -> street 1 - [ name , no of house , senior people ] street 2 - [ name , no of house , senior people ] city 2 -> street 1 - [ name , no of…
Sam
  • 95
  • 1
  • 9
4
votes
2 answers

How to store Hash of Hashes in Moose?

i was wondering, what is the best way to store Hash of Hashes in Moose. Lets take for example a Hash like this: my %hash = ('step1' => {'extraction' => \$object1, 'analysis' => \$object2}, 'step2' =>…
keanni
  • 528
  • 6
  • 15
3
votes
2 answers

Ruby hash of hash of hash

How can I have a hash of hash of hash? My test returns undefined method `[]' for nil:NilClass (NoMethodError) Any tips? found = Hash.new() x = 1; while x < 4 do found[x] = Hash.new() y = 1 while y < 4 do found[x][y] = Hash.new() …
Alfons
  • 311
  • 1
  • 8
  • 17
3
votes
5 answers

Access nested hash in Perl HoH without using keys()?

Consider the following HoH: $h = { a => { 1 => x }, b => { 2 => y }, ... } Is there a way to check whether a hash key exists on the second nested level without calling keys(%$h)? For example, I want to say…
MisterEd
  • 1,725
  • 1
  • 14
  • 15
3
votes
4 answers

Perl: Create hash of hashes, last key as a reference to an array

http://codepad.org/8fJG5XaB Need a little help creating hashrefs of hashrefs, with the last key as a reference to an array. use Data::Dumper; my $foo = "a:b:c:d:a"; my $bar = "a:b:c:d:z"; my $hoh = {}; sub createHash { my…
vol7ron
  • 40,809
  • 21
  • 119
  • 172
3
votes
2 answers

How to construct a hash of hashes

I need to compare two hashes, but I can't get the inner set of keys... my %HASH = ('first'=>{'A'=>50, 'B'=>40, 'C'=>30}, 'second'=>{'A'=>-30, 'B'=>-15, 'C'=>9}); foreach my $key (keys(%HASH)) { my %innerhash = $options{$key}; …
Eric Fossum
  • 2,395
  • 4
  • 26
  • 50
3
votes
4 answers

Merge Ruby nested hashes with same keys

I have several hashes in Ruby which have nested hashes inside of them an share very similar structure. They look something like this: a = { "year_1": { "sub_type_a": { "label1": value1 } }, "year_2": { …
mmvsbg
  • 3,570
  • 17
  • 52
  • 73
3
votes
6 answers

Nested hash defined?()

What's the most concise way to determine if @hash[:key1][:key2] is defined, that does not throw an error if @hash or @hash[:key1] are nil? defined?(@hash[:key1][:key2]) returns True if @hash[:key1] exists (it does not determine whether :key2 is…
Brian Jordan
  • 2,377
  • 3
  • 21
  • 29
3
votes
3 answers

Create hash-of-hashes using structure of the default hash

I have the following code: default = {:id => 0, :detail =>{:name=>"Default", :id => ""}} employees = {} nr = (0..3).to_a nr.each do |n| employee = default employee[:id] = n employee[:detail][:name] = "Default #{n}" …
3
votes
2 answers

How do I recursively define a Hash in Ruby from supplied arguments?

This snippet of code populates an @options hash. values is an Array which contains zero or more heterogeneous items. If you invoke populate with arguments that are Hash entries, it uses the value you specify for each entry to assume a default…
3
votes
4 answers

Perl sort by hash value in array of hashes or hash of hashes

Can anybody tell me what I am doing wrong here? I have tried just about every possible combination of array / hash type and sort query I can think of and cannot seem to get this to work. I am trying to sort the hash ref below by value1 : my $test =…
someuser
  • 2,279
  • 4
  • 28
  • 39
3
votes
3 answers

Ruby quickly make an array out of part of a hash of hashes?

Okay, so if I have a hash of hashes to represent books like this: Books = {"Harry Potter" => {"Genre" => Fantasy, "Author" => "Rowling"}, "Lord of the Rings" => {"Genre" => Fantasy, "Author" => "Tolkien"} ... } Is there any way that I could…
user3380049
  • 139
  • 1
  • 10
3
votes
1 answer

merge some complex hashes in ruby

I'd like to merge the following hashes together. h1 = {"201201" => {:received => 2}, "201202" => {:received => 4 }} h2 = {"201201" => {:closed => 1}, "201202" => {:closed => 1 }} particularly, my expected result is: h1 = {"201201" =>…
Sarun Sermsuwan
  • 3,608
  • 5
  • 34
  • 46
2
votes
5 answers

Is saving a hash in another hash common practice?

I'd like to save some hash objects to a collection (in the Java world think of it as a List). I search online to see if there is a similar data structure in Ruby and have found none. For the moment being I've been trying to save hash a[] into hash…
Rafael
  • 1,061
  • 4
  • 16
  • 32
2
votes
3 answers

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

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…
Pro-grammar
  • 365
  • 2
  • 17
1
2
3
9 10