[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

hash of hashes

Paul Argentoff

10/3/2003 10:05:00 AM

Hi all.

How can I create $Subject for future population?
--
Yours truly, WBR, Paul Argentoff.
Jabber: paul@jabber.rtelekom.ru
RIPE: PA1291-RIPE


15 Answers

Paul Argentoff

10/3/2003 10:35:00 AM

0

On Friday 03 October 2003 14:04, Paul Argentoff wrote:

> How can I create $Subject for future population?

Let's put it this way: I need to create a nested hash which will be populated
later by referencing the keys in the subhashes. How can I do this?
--
Yours truly, WBR, Paul Argentoff.
Jabber: paul@jabber.rtelekom.ru
RIPE: PA1291-RIPE


Emmanuel Touzery

10/3/2003 10:48:00 AM

0

Paul Argentoff wrote:

>On Friday 03 October 2003 14:04, Paul Argentoff wrote:
>
>
>
>>How can I create $Subject for future population?
>>
>>
>
>Let's put it this way: I need to create a nested hash which will be populated
>later by referencing the keys in the subhashes. How can I do this?
>
>
not sure i understand your question. does this help you?

irb(main):001:0> a = {}
{}
irb(main):002:0> a["blue"] = {}
{}
irb(main):003:0> a["blue"]["red"] = 3
3
irb(main):004:0> a["blue"]
{"red"=>3}
irb(main):005:0> a["blue"]["red"]
3
irb(main):006:0>



Paul Argentoff

10/3/2003 11:03:00 AM

0

On Friday 03 October 2003 14:48, Emmanuel Touzery wrote:

> irb(main):001:0> a = {}
> {}
> irb(main):002:0> a["blue"] = {}
> {}
> irb(main):003:0> a["blue"]["red"] = 3
> 3
> irb(main):004:0> a["blue"]
> {"red"=>3}
> irb(main):005:0> a["blue"]["red"]
> 3
> irb(main):006:0>

I need something like this:

a = {{}} # this doesn't work, but i've just written it here to emph. the
# subject
...
# somewhere in the iteration
a[foo][bar] = baz
...
--
Yours truly, WBR, Paul Argentoff.
Jabber: paul@jabber.rtelekom.ru
RIPE: PA1291-RIPE


Emmanuel Touzery

10/3/2003 11:07:00 AM

0

Paul Argentoff wrote:

>On Friday 03 October 2003 14:48, Emmanuel Touzery wrote:
>
>
>
>>irb(main):001:0> a = {}
>>{}
>>irb(main):002:0> a["blue"] = {}
>>{}
>>irb(main):003:0> a["blue"]["red"] = 3
>>3
>>irb(main):004:0> a["blue"]
>>{"red"=>3}
>>irb(main):005:0> a["blue"]["red"]
>>3
>>irb(main):006:0>
>>
>>
>
>I need something like this:
>
>a = {{}} # this doesn't work, but i've just written it here to emph. the
> # subject
>...
># somewhere in the iteration
>a[foo][bar] = baz
>...
>
>
i think you need ruby 1.8's block form for creating hashes. i asked that
question recently, and here is the snippet from Guy Decoux:

svg% ruby -e 'has = Hash.new {|h,k| h[k]={}}; has["blue"]["red"] = 1; p has'
{"blue"=>{"red"=>1}}
svg%




Paul Argentoff

10/3/2003 11:18:00 AM

0

On Friday 03 October 2003 14:48, Emmanuel Touzery wrote:

> not sure i understand your question. does this help you?
>
> irb(main):001:0> a = {}
> {}
> irb(main):002:0> a["blue"] = {}
> {}
> irb(main):003:0> a["blue"]["red"] = 3
> 3
> irb(main):004:0> a["blue"]
> {"red"=>3}
> irb(main):005:0> a["blue"]["red"]
> 3
> irb(main):006:0>

Here is my example:

irb(main):008:0> a = Hash.new(Hash.new(Array.new(2,0)))
=> {}
irb(main):009:0> a['foo']['bar'][1] = 2
=> 2
irb(main):010:0> a['foo']['bar']
=> [0, 2]
irb(main):011:0> a.keys
=> []
irb(main):012:0>

After this I can't iterate over a's keys -- what I've written the whole mess
for :(
--
Yours truly, WBR, Paul Argentoff.
Jabber: paul@jabber.rtelekom.ru
RIPE: PA1291-RIPE


Joel VanderWerf

10/3/2003 2:37:00 PM

0

Paul Argentoff wrote:
...
> I need something like this:
>
> a = {{}} # this doesn't work, but i've just written it here to emph. the
> # subject
> ...
> # somewhere in the iteration
> a[foo][bar] = baz
> ...

hash_maker = proc do |h, k|
h[k] = Hash.new(&hash_maker)
end

h = Hash.new(&hash_maker)

h[1][2][3][4][5] = 6

p h # {1=>{2=>{3=>{4=>{5=>6}}}}}


Paul Argentoff

10/3/2003 4:30:00 PM

0

On Friday 03 October 2003 18:37, Joel VanderWerf wrote:

> hash_maker = proc do |h, k|
>    h[k] = Hash.new(&hash_maker)
> end

:))) Exmaple is worthy of a Haskell tutorial :)))

The problem is, afaik, that I tried to do THAT with Ruby 1.6 :-/
--
Yours truly, WBR, Paul Argentoff.
Jabber: paul@jabber.rtelekom.ru
RIPE: PA1291-RIPE


Joel VanderWerf

10/3/2003 8:23:00 PM

0

Paul Argentoff wrote:
> On Friday 03 October 2003 18:37, Joel VanderWerf wrote:
>
>
>>hash_maker = proc do |h, k|
>> h[k] = Hash.new(&hash_maker)
>>end
>
>
> :))) Exmaple is worthy of a Haskell tutorial :)))
>
> The problem is, afaik, that I tried to do THAT with Ruby 1.6 :-/

It might work with the shim module, from RAA.


dagbrown

10/4/2003 1:37:00 AM

0

In article <200310031434.31907.argentoff@rtelekom.ru>,
Paul Argentoff <argentoff@rtelekom.ru> wrote:
: On Friday 03 October 2003 14:04, Paul Argentoff wrote:
:
: > How can I create $Subject for future population?
:
: Let's put it this way: I need to create a nested hash which will be populated
: later by referencing the keys in the subhashes. How can I do this?

You mean, er,

irb(main):001:0> a=Hash.new({})
=> {}
irb(main):002:0> a['foo']
=> {}

?

--Dave
--
$@=q$!???@!!?@!!!@?*!?@?!@???@?@!!!!@!@!?!*!??!@!@!?!@!?!!*!!!!@!?@?!?!@?!?@!@
!?!$;@*=split//,qq%k842<.6.5)*\$0:/3+&k1p\0137"#>9!,%^"[]@"x10;$!=!($?=0);for$*
(split//,$@){if($*ne'!'&&$*ne"?"){print$!?uc($*[$?]):$*[$?]if$*[$?];$?=!($!=0)
==0}else{$?*=2;$?++;$?++if$*eq'?';}print(pack(c,32))if$*eq'*';}print"$*[$?],\n"

Joel VanderWerf

10/4/2003 3:14:00 AM

0

Dave Brown wrote:
> In article <200310031434.31907.argentoff@rtelekom.ru>,
> Paul Argentoff <argentoff@rtelekom.ru> wrote:
> : On Friday 03 October 2003 14:04, Paul Argentoff wrote:
> :
> : > How can I create $Subject for future population?
> :
> : Let's put it this way: I need to create a nested hash which will be populated
> : later by referencing the keys in the subhashes. How can I do this?
>
> You mean, er,
>
> irb(main):001:0> a=Hash.new({})
> => {}
> irb(main):002:0> a['foo']
> => {}
>
> ?
>
> --Dave

That's like, a real bad trip, dude:

irb(main):002:0> a=Hash.new({})
=> {}
irb(main):003:0> a[1]
=> {}
irb(main):004:0> a[1]["x"] = "y"
=> "y"
irb(main):005:0> a
=> {}
irb(main):006:0> a[1]
=> {"x"=>"y"}
irb(main):007:0> a[1] = 2
=> 2
irb(main):008:0> a[2]
=> {"x"=>"y"}
irb(main):009:0> a[1]
=> 2

It would help to do

a = Hash.new {{}}

since you get a new hash each time...