hash-of-hashes

Return single :key from nested hash in ruby

Deadly 提交于 2019-12-16 18:06:13
问题 From psd.rb gem by LayerVault, I get a hash that contains all the layers and groups of the psd in nested hashes. Here is a gist of the hash. I want to retrieve a certain key from that hash. Is there a way to do this and store those keys values in an array? 回答1: def values(hsh, key) return [] if !hsh.kind_of? Hash v = hsh[key] ? [hsh[key]] : [] hsh.values.select{|i| i.kind_of? Hash or i.kind_of? Array}.each do |val| if val.kind_of? Hash v+= values(val, key) else val.each {|i| v+= values(i, key

How can I read in a variable/value which is a runtime parameter that is inside a constant list of hashes?

我与影子孤独终老i 提交于 2019-12-14 03:27:09
问题 I have tried putting the variables in the string but it reads as blank when I run the program. Here is an example of what I'm working with: use constant { #list will contain more errors ERROR_SW => { errorCode => 727, message => "Not able to ping switch $switch_ip in $timeout seconds", fatal => 1, web_page => 'http://www.errorsolution.com/727', } }; sub error_post { my ($error) = @_; print($error->{message}); } error_post(ERROR_SW); I simply want to post the error with the variable values

How to extract key name from a hash of hash?

我是研究僧i 提交于 2019-12-12 16:56:46
问题 I have following hash of hash : %HoH = ( flintstones => { husband => "fred", pal => "barney", }, jetsons => { husband => "george", wife => "jane", his boy => "elroy", }, simpsons => { husband => "homer", wife => "marge", kid => "bart", }, ); How to iterate over each inner hash (say flintstones) and also extract the key names (husband, pal) and corresponding vales for each such iteration? 回答1: for my $k (keys %{ $HoH{flintstones} }) { my $v = $HoH{flintstones}{$k}; print "key is $k; value is

How to get the key associated with a hash reference's key in a hash of hashes?

≯℡__Kan透↙ 提交于 2019-12-11 01:11:49
问题 In an attempt to help me learn Perl, I built the following data structure, where the inner hash (/DriveA/archive, etc.) is a hash reference: #The contents of the %properties hash of hashes #The inner hash is a hash reference to a hash named %attributes $VAR1 = { '/DriveA' => { '/DriveA/archive/' => { 'MaxSize' => '20GB', 'Size' => '19GB', 'Free' => '5' }, '/DriveA/current/' => { 'MaxSize' => '20GB', 'Size' => '12GB', 'Free' => '40' } }, '/DriveB' => { '/DriveB/archive/' => { 'MaxSize' => '8GB

merge some complex hashes in ruby

岁酱吖の 提交于 2019-12-07 17:20:35
问题 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" => {:received => 2, :closed => 1}, "201202" => {:received => 4, :closed => 1 }} I have tried every way as: h = h1.merge(h2){|key, first, second| {first , second} } unfortunately, neither seemed to work out fine for me. any advice would be really appreciated. 回答1:

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

孤者浪人 提交于 2019-12-06 08:14:53
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_loc => 24, y_loc => 72, } rect_size => { width => 2, height => 4, } sounds => { attack => "goblin

merge some complex hashes in ruby

戏子无情 提交于 2019-12-05 20:22:42
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" => {:received => 2, :closed => 1}, "201202" => {:received => 4, :closed => 1 }} I have tried every way as: h = h1.merge(h2){|key, first, second| {first , second} } unfortunately, neither seemed to work out fine for me. any advice would be really appreciated. This should work for you: h = h1.merge(h2){|key, first, second| first.merge(second)} 来源: https://stackoverflow

extracting specific value from a multidimensional hash in ruby by key name

依然范特西╮ 提交于 2019-12-05 10:58:47
问题 let's say i have a multidimensional hash, and in one of the subhashes i have a key=>value pair which i need to retrieve by key. how can i do it? example hashes: h={:x=>1,:y=>2,:z=>{:a=>{:k=>"needle"}}} h={:k=>"needle"} key is always :k, and i need to get "needle" i noticed that there is no "flatten" function for hashes in ruby 1.8, but if it'd be there, i imagine i'd just do h.flatten[:k] i imagine i need to write a recursive function for that? thanks 回答1: You can always write your own

How to store Hash of Hashes in Moose?

女生的网名这么多〃 提交于 2019-12-04 11:25:39
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' => {'extraction' => \$object3, 'analysis' => \$object4}); but i want to save this one in a moose attribute. How should i organize the access (reading, writing) on this. Examples on the net are mostly for "flat" hashes. But then you can use helpers like Moose::Meta::Attribute::Native::Trait::Hash. Is there something similar for hash of hashes? Reason for this is, that i want to iterate over the step

extracting specific value from a multidimensional hash in ruby by key name

☆樱花仙子☆ 提交于 2019-12-03 21:53:11
let's say i have a multidimensional hash, and in one of the subhashes i have a key=>value pair which i need to retrieve by key. how can i do it? example hashes: h={:x=>1,:y=>2,:z=>{:a=>{:k=>"needle"}}} h={:k=>"needle"} key is always :k, and i need to get "needle" i noticed that there is no "flatten" function for hashes in ruby 1.8, but if it'd be there, i imagine i'd just do h.flatten[:k] i imagine i need to write a recursive function for that? thanks You can always write your own mission-specific extension to Hash which does the dirty work for you: class Hash def recursive_find_by_key(key) #