[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

how to make a[2][2][3]=4 work?

gz zz

6/12/2007 6:30:00 AM

a=Hash.new{|h,k|
h[k]={}
}

p a[1]=1

a[2][1]=2
#a[2][2][3]=4
p a

--
Posted via http://www.ruby-....

16 Answers

gz zz

6/12/2007 6:35:00 AM

0

I know this can work,but I think it is suck
a=Hash.new{|h,k|
h[k]=Hash.new{|h,k|h[k]=Hash.new{|h,k|h[k]={}}}
}

p a[1]=1

a[2][1]=2
a[2][2][3]=4
a[3][1][1][1]=1
p a


--
Posted via http://www.ruby-....

Daniel Sheppard

6/12/2007 6:42:00 AM

0

> I know this can work,but I think it is suck
> a=Hash.new{|h,k|
> h[k]=Hash.new{|h,k|h[k]=Hash.new{|h,k|h[k]={}}}
> }
>
> p a[1]=1
>
> a[2][1]=2
> a[2][2][3]=4
> a[3][1][1][1]=1
> p a

build_hash = proc {|h,k|h[k] = Hash.new &build_hash }
a = Hash.new &build_hash

Sylvain Joyeux

6/12/2007 6:50:00 AM

0

def recursive_hash
Hash.new { |h, k| h[k] = recursive_hash }
end
a = recursive_hash

Or even

class Hash
def self.recursive
new { |h, k| h[k] = recursive }
end
end

which gives

>> a = Hash.recursive
=>
>> a[1][2][3][4] = 1
=> 1
>> puts a.inspect
{1=>{2=>{3=>{4=>1}}}}

--
Sylvain Joyeux

Daniel Martin

6/12/2007 12:21:00 PM

0

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}

gz zz <gpygood@126.com> writes:

> a[2][1]=2
> a[2][2][3]=4
> a[3][1][1][1]=1
> p a

irb(main):001:0> a = Hash.new{|h,k| h[k]=Hash.new &h.default_proc}
=> {}
irb(main):002:0> a[2][1]=2
=> 2
irb(main):003:0> a[2][2][3]=4
=> 4
irb(main):004:0> a[3][1][1][1]=1
=> 1
irb(main):005:0> p a
{2=>{1=>2, 2=>{3=>4}}, 3=>{1=>{1=>{1=>1}}}}
=> nil


--
s=%q( Daniel Martin -- martin@snowplow.org
puts "s=%q(#{s})",s.to_a.last )
puts "s=%q(#{s})",s.to_a.last

Jenda Krynicky

6/12/2007 12:51:00 PM

0

gz zz wrote:
> 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.

Jenda

--
Posted via http://www.ruby-....

Martin DeMello

6/12/2007 1:24:00 PM

0

On 6/12/07, Jenda Krynicky <jenda@cpan.org> wrote:
>
> 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.

That's okay, it happens to the best of us.

martin

Gavin Kistner

6/12/2007 3:52:00 PM

0

On Jun 12, 6:21 am, Daniel Martin <mar...@snowplow.org> wrote:
> 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}

Thanks for this - I didn't know about the default_proc method. Quite
elegant in this case.

Fsormok Fsormok

7/20/2007 5:58:00 PM

0


> 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 :-)

--fsormok


--
Posted via http://www.ruby-....

Bill Kelly

7/20/2007 7:48:00 PM

0


From: "Fsormok Fsormok" <fsormok@inbox.ru>
>
>> 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 :-)

You can do that in Ruby, if you want to.
See: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...

irb(main):011:0> int = Hash.new{|h,k| h[k]=Hash.new &h.default_proc}
=> {}
irb(main):012:0> int[:eth0][:ip] = "10.0.0.1"
irb(main):013:0> int[:eth0][:mask] = "255.255.255.0"
irb(main):014:0> int[:eth0][:cidr] = "24"
irb(main):015:0> int[:eth1][:ip] = "10.0.1.1"
irb(main):016:0> int[:eth1][:mask] = "255.255.255.0"
irb(main):017:0> int[:eth1][:cidr] = "24"
irb(main):018:0> int
=> {:eth0=>{:ip=>"10.0.0.1", :mask=>"255.255.255.0", :cidr=>"24"}, :eth1=>{:ip=>"10.0.1.1", :mask=>"255.255.255.0", :cidr=>"24"}}


Another possibility might be OpenStruct.

irb(main):026:0> require 'ostruct'
=> true
irb(main):027:0> int = Hash.new{|h,k| h[k]=OpenStruct.new}
=> {}
irb(main):028:0> int[:eth0].ip = "10.0.0.1"
irb(main):029:0> int[:eth0].mask = "255.255.255.0"
irb(main):030:0> int[:eth0].cidr = "24"
irb(main):031:0> int[:eth1].ip = "10.0.1.1"
irb(main):032:0> int[:eth1].mask = "255.255.255.0"
irb(main):033:0> int[:eth1].cidr = "24"
irb(main):034:0> int
=> {:eth0=>#<OpenStruct ip="10.0.0.1", mask="255.255.255.0", cidr="24">, :eth1=>#<OpenStruct ip="10.0.1.1", mask="255.255.255.0",
cidr="24">}


Regards,

Bill



William James

7/20/2007 8:24:00 PM

0

On Jun 12, 1:29 am, gz zz <gpyg...@126.com> wrote:
> a=Hash.new{|h,k|
> h[k]={}
>
> }
>
> p a[1]=1
>
> a[2][1]=2
> #a[2][2][3]=4


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