G
gz zz
a=Hash.new{|h,k|
h[k]={}
}
p a[1]=1
a[2][1]=2
#a[2][2][3]=4
p a
h[k]={}
}
p a[1]=1
a[2][1]=2
#a[2][2][3]=4
p a
a=3DHash.new{|h,k|
h[k]=3DHash.new{|h,k|h[k]=3DHash.new{|h,k|h[k]=3D{}}}
}
=20
p a[1]=3D1
=20
a[2][1]=3D2
a[2][2][3]=3D4
a[3][1][1][1]=3D1
p a
{1=>{2=>{3=>{4=>1}}}}a = Hash.recursive =>
a[1][2][3][4] = 1 => 1
puts a.inspect
gz zz said:a[2][1]=2
a[2][2][3]=4
a[3][1][1][1]=1
p a
gz said:a=Hash.new{|h,k|
h[k]={}
}
p a[1]=1
a[2][1]=2
#a[2][2][3]=4
p a
Sure. Use Perl.
Oops, sorry. I forgot, datastructure autovivification is wrong,
variables springing into life (or maybe not, read the rest of the method
to find out) inadvertently are good.
In addition to what's already been posted, there's this, which doesn't
require any extra variables or methods:
a = Hash.new{|h,k| h[k]=Hash.new &h.default_proc}
Sure. Use Perl.
From: "Fsormok Fsormok said:Sure. Use Perl.
What's the ruby-way of such things?
e.g.:
int[eth0][ip] = "10.0.0.1"
int[eth0][mask] = "255.255.255.0"
int[eth0][cidr] = "24"
int[eth1][ip] = "10.0.1.1"
int[eth1][mask] = "255.255.255.0"
int[eth1][cidr] = "24"
.
.
.
I like it if I have all these informations handy within one hash. But
even for me as a realy ruby-newby this looks much more like perl than
ruby.
It would be great to hear your thoughts how this would be done "the ruby
way"...
clean and elegant
a=Hash.new{|h,k|
h[k]={}
}
p a[1]=1
a[2][1]=2
#a[2][2][3]=4
irb(main):026:0> require 'ostruct'
irb(main):027:0> int = Hash.new{|h,k| h[k]=OpenStruct.new}
a = {}
==>{}
a[1] = 1
==>1
a[ [2,1] ] = 2
==>2
a[ [2,2,3] ] = 4
==>4
p a
{[2, 2, 3]=>4, [2, 1]=>2, 1=>1}
==>nil
a = Hash.new{|h,k| h[k]=Hash.new &h.default_proc}irb(main):026:0> require 'ostruct'
irb(main):027:0> int = Hash.new{|h,k| h[k]=OpenStruct.new}a = {}
==>{}
a[1] = 1
==>1
a[ [2,1] ] = 2
==>2
a[ [2,2,3] ] = 4
==>4
p a
{[2, 2, 3]=>4, [2, 1]=>2, 1=>1}
==>nil
Tanks for the replies.
I'm just a little bit confused because in ruby everything is lean and
clean but when it comes to hashes of hashes it gets kind of complicated.
For me it looks like ruby has another "concept" of solving such things.
Sure. Use Perl.
What's the ruby-way of such things?
e.g.:
int[eth0][ip] = "10.0.0.1"
int[eth0][mask] = "255.255.255.0"
int[eth0][cidr] = "24"
int[eth1][ip] = "10.0.1.1"
int[eth1][mask] = "255.255.255.0"
int[eth1][cidr] = "24"
.
.
.
I like it if I have all these informations handy within one hash. But
even for me as a realy ruby-newby this looks much more like perl than
ruby.
It would be great to hear your thoughts how this would be done "the ruby
way"...
clean and elegant
In my personal opinion, multilevel hashes aren't clean and elegant in
any language.
2007/7/23 said:a = Hash.new{|h,k| h[k]=Hash.new &h.default_proc}irb(main):026:0> require 'ostruct'
irb(main):027:0> int = Hash.new{|h,k| h[k]=OpenStruct.new}a = {}
==>{}
a[1] = 1
==>1
a[ [2,1] ] = 2
==>2
a[ [2,2,3] ] = 4
==>4
p a
{[2, 2, 3]=>4, [2, 1]=>2, 1=>1}
==>nil
Tanks for the replies.
I'm just a little bit confused because in ruby everything is lean and
clean but when it comes to hashes of hashes it gets kind of complicated.
For me it looks like ruby has another "concept" of solving such things.
{1=>{2=>3}, 4=>5}miss = lambda {|h,k| h[k] = Hash.new(&miss)}
hash = Hash.new(&miss)
hash[1][2]=3
hash[4]=5
p hash
XXX
Daniel said:In addition to what's already been posted, there's this, which doesn't
require any extra variables or methods:
a = Hash.new{|h,k| h[k]=Hash.new &h.default_proc}
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.