From: John Carter Date: 2005-05-27T07:37:29+09:00 Subject: Re: hash of hash and complex data structure On Fri, 27 May 2005, ngoc wrote: > I am perl programmer, looking for new tools and interested in Ruby > How in ruby similar to perl > $a{$b}{$c}{$d} = 1; Ruby doesn't quite have the auto-vivification that perl has. You have to be a little more explicit about specifying what the behaviour of the hash on accessing a uninited key should be eg.. Perl $a{fred}++ Will assume "quotes" around the fred, and the autovivify the $a{"fred"} as 0 as you are adding one to it. Ruby doesn't quite do either. You get something similar in Ruby by.. a = Hash.new(0) a["fred"]+=1 You can get fancier in Ruby with... a = Hash.new {|hash,key| hash[key] = Hash.new{ |hash,key| hash[key] = Hash.new(0) }} a["tom"]["dick"]["harry"] += 1 On the other hand autovivifivation is a _very_ suprising feature of perl that took me years to find out about. I prefer ruby's more explicit approach. John Carter Phone : (64)(3) 358 6639 Tait Electronics Fax : (64)(3) 359 4632 PO Box 1645 Christchurch Email : john.carter@tait.co.nz New Zealand Carter's Clarification of Murphy's Law. "Things only ever go right so that they may go more spectacularly wrong later." From this principle, all of life and physics may be deduced.