From: Joel VanderWerf Date: 2005-05-26T07:27:33+09:00 Subject: Re: Multilevel hash Joel VanderWerf wrote: > aartist wrote: > >> With perl it's easy to write: >> >> $Q{$date}{$song_played} += 1 >> >> How I can accomplish the same thing with ruby? >> >> Thanks! >> >> http://pleac.sourceforge.net/pleac_ruby/index.html didn't help much >> here. >> > > irb(main):001:0> mh = Hash.new {|h,k| h[k] = {} } > => {} > irb(main):002:0> mh[1][2] = 3 > => 3 > irb(main):003:0> mh > => {1=>{2=>3}} And if you want automatic zeros: irb(main):010:0> mh0 = Hash.new {|h,k| h[k] = Hash.new(0)} => {} irb(main):011:0> mh0[1][2] += 1 => 1 irb(main):012:0> mh0 => {1=>{2=>1}}