Ask a Question related to Ruby, Design and Development.
-
Paul Argentoff #1
hash of hashes
Hi all.
How can I create $Subject for future population?
--
Yours truly, WBR, Paul Argentoff.
Jabber: [email]paul@jabber.rtelekom.ru[/email]
RIPE: PA1291-RIPE
Paul Argentoff Guest
-
Sorting Hash of Hashes with HEAP module
Hi, I would like to use the Heap module from CPAN to sort Hash of Hashes by values, keeping track of what pairs of keys belong to what values. ... -
XML parse - hash of hashes
Greetings, I am fairly new to Perl and to XML and I’m trying to (1) parse a XML document (snippet attached), (2) update specified data using a... -
Accesing hash of hashes
Hello: I am trying to create and access a multidimensional hash. For example the following works ====== $route {$routeDest} = $cost ; print... -
Sort a hash based on values in the hash stored as arrays of hashes
Hmm. I'm not quite sure if I got the subject right, but I'll try to explain. :-) I've got a hash of elements stored like this: $VAR1 = {... -
iterating through hash of hashes
I'd like to iterate through a hash of hashes and get all the values. How do I do that? The following works for me. But it seems a bit convoluted... -
Paul Argentoff #2
Re: hash of hashes
On Friday 03 October 2003 14:04, Paul Argentoff wrote:
Let's put it this way: I need to create a nested hash which will be populated> How can I create $Subject for future population?
later by referencing the keys in the subhashes. How can I do this?
--
Yours truly, WBR, Paul Argentoff.
Jabber: [email]paul@jabber.rtelekom.ru[/email]
RIPE: PA1291-RIPE
Paul Argentoff Guest
-
Emmanuel Touzery #3
Re: hash of hashes
Paul Argentoff wrote:
not sure i understand your question. does this help you?>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?
>
>
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>
Emmanuel Touzery Guest
-
Paul Argentoff #4
Re: hash of hashes
On Friday 03 October 2003 14:48, Emmanuel Touzery wrote:
I need something like this:> 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>
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: [email]paul@jabber.rtelekom.ru[/email]
RIPE: PA1291-RIPE
Paul Argentoff Guest
-
Emmanuel Touzery #5
Re: hash of hashes
Paul Argentoff wrote:
i think you need ruby 1.8's block form for creating hashes. i asked that>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
>...
>
>
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%
Emmanuel Touzery Guest
-
Paul Argentoff #6
Re: hash of hashes
On Friday 03 October 2003 14:48, Emmanuel Touzery wrote:
Here is my example:> 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>
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: [email]paul@jabber.rtelekom.ru[/email]
RIPE: PA1291-RIPE
Paul Argentoff Guest
-
Austin Ziegler #7
Re: hash of hashes
On Fri, 3 Oct 2003 20:17:50 +0900, Paul Argentoff wrote:
This won't work because you're not doing what you're expecting. What> On Friday 03 October 2003 14:48, Emmanuel Touzery wrote:> Here is my example:>> not sure i understand your question. does this help you?
> irb(main):008:0> a = Hash.new(Hash.new(Array.new(2,0)))
you're saying is that a nil key will return a particular Hash where
nil keys return a particular array.
This would be like saying:
arry = Array.new(2, 0)
hash = Hash.new(arry)
a = Hash.new(hash)
a['foo'] # => hash
a['foo']['bar'] # => arry
What you want is:
a = Hash.new do |h, k|
h[k] = Hash.new do |h1, k1|
h1[k1] = Array.new(2, 0)
end
end
This only works with 1.8 or bettter. Ruby does not auto-vivify
hashes or arrays like Perl. Note further that if you do something
like:
a = Hash.new { |h, k| h[k] = :val; nil }
You will get a perhaps surprising -- but entirely correct -- result:
a['foo'] # => nil
a['foo'] # => :val
When you attempt to access a nonexistant hash key, the default value
is returned. In this case, it's nil. However, in the block you have
also set a side effect of setting the hash key provided to :val, so
the next time you access 'foo', it's no longer nonexistant.
-austin
--
austin ziegler * [email]austin@halostatue.ca[/email] * Toronto, ON, Canada
software designer * pragmatic programmer * 2003.10.03
* 09.15.49
Austin Ziegler Guest
-
Joel VanderWerf #8
Re: hash of hashes
Paul Argentoff wrote:
...hash_maker = proc do |h, k|> 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
> ...
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}}}}}
Joel VanderWerf Guest
-
Paul Argentoff #9
Re: hash of hashes
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: [email]paul@jabber.rtelekom.ru[/email]
RIPE: PA1291-RIPE
Paul Argentoff Guest
-
Joel VanderWerf #10
Re: hash of hashes
Paul Argentoff wrote:
It might work with the shim module, from RAA.> 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 :-/
Joel VanderWerf Guest
-
Austin Ziegler #11
Re: hash of hashes
On Sat, 4 Oct 2003 10:52:51 +0900, 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
> : populatedlater by referencing the keys in the subhashes. How can I do
> : this?> You mean, er,I don't think that's what you want:> irb(main):001:0> a=Hash.new({})
> => {}
> irb(main):002:0> a['foo']
> => {}
irb(main):001:0> a = Hash.new({})
=> {}
irb(main):002:0> a['foo'].id
=> 21054224
irb(main):003:0> a['bar'].id
=> 21054224
-austin
--
austin ziegler * [email]austin@halostatue.ca[/email] * Toronto, ON, Canada
software designer * pragmatic programmer * 2003.10.03
* 22.22.45
Austin Ziegler Guest
-
Joel VanderWerf #12
Re: hash of hashes
Dave Brown wrote:
That's like, a real bad trip, dude:> 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
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...
Joel VanderWerf Guest
-
Martin DeMello #13
Re: hash of hashes
Joel VanderWerf <vjoel@path.berkeley.edu> wrote:
My head hurts>
> 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}}}}}
That needs to go on the Ruby Idioms page.
martin
Martin DeMello Guest
-
Simon Strandgaard #14
Re: hash of hashes
On Sat, 04 Oct 2003 06:25:35 +0000, Martin DeMello wrote:
> Joel VanderWerf <vjoel@path.berkeley.edu> wrote:>>>
>> 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}}}}}
> My head hurts
That needs to go on the Ruby Idioms page.
Wew nice trick indeed :-)
I have added it to the StandardClassExtension page:
[url]http://www.rubygarden.org/ruby?StandardClassExtensions/Hash[/url]
--
Simon Strandgaard
Simon Strandgaard Guest
-
Robert Klemme #15
Re: hash of hashes
"Paul Argentoff" <argentoff@rtelekom.ru> schrieb im Newsbeitrag
news:200310031517.45708.argentoff@rtelekom.ru...mess> 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 wholeThat's because the form Hash.new( obj ) just replaces the nil return value> for
for keys that are not found. In your example you have a hash that returns a
hash for unknown keys that in turn returns an array for unknown keys. You
definitely need the block form. Or you have to wrap or sub class the hash
if you want to use pre 1.8 ruby versions.
Regards
robert
Robert Klemme Guest
-
Robert Klemme #16
Re: hash of hashes
"Joel VanderWerf" <vjoel@PATH.Berkeley.EDU> schrieb im Newsbeitrag
news:3F7E3AF2.2050701@path.berkeley.edu...populated> 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 beNot really:>> > : 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...
irb(main):001:0> h=Hash.new{{}}
=> {}
irb(main):002:0> h[1][2]="foo"
=> "foo"
irb(main):003:0> h
=> {}
irb(main):004:0>
You need a = Hash.new {|h,k| h[k]={}} as pointed out already.
robert
>
>Robert Klemme Guest
-
Joel VanderWerf #17
Re: hash of hashes
Robert Klemme wrote:
...> "Joel VanderWerf" <vjoel@PATH.Berkeley.EDU> schrieb im NewsbeitragOops. Of course you're right, since the value returned by the block is>>>It would help to do
>>
>>a = Hash.new {{}}
>>
>>since you get a new hash each time...
>
> Not really:
never assigned to any part of the hash. You always have to do that yourself.
Joel VanderWerf Guest
-
Robert Klemme #18
Re: hash of hashes
"Joel VanderWerf" <vjoel@PATH.Berkeley.EDU> schrieb im Newsbeitrag
news:3F7F0FF8.1030708@path.berkeley.edu...yourself.> Robert Klemme wrote:> ..> > "Joel VanderWerf" <vjoel@PATH.Berkeley.EDU> schrieb im Newsbeitrag>> >> >>It would help to do
> >>
> >>a = Hash.new {{}}
> >>
> >>since you get a new hash each time...
> >
> > Not really:
> Oops. Of course you're right, since the value returned by the block is
> never assigned to any part of the hash. You always have to do that
.... and it's always the same. Something not wanted in this case.
robert
Robert Klemme Guest
-
smile #19
Re: hash of hashes
Thank you for the information,[url=http://www.nflwholesalejerseys.com] wholesale nfl football jerseys [/url] let me know so much about your information
smile Guest
-
Clement.laurencia #20
Re: hash of hashes
Umm... this sounds suspiciously like a homework assignment.
[url=http://www.townandcitydating.com/]singles online[/url] | [url=http://www.townandcitydating.com/]singles in London[/url] | [url=http://www.townandcitydating.com/]online dating service[/url]
Junior Member
- Join Date
- Mar 2012
- Posts
- 9



Reply With Quote

