Ask a Question related to Ruby, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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. ...
    2. 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...
    3. Accesing hash of hashes
      Hello: I am trying to create and access a multidimensional hash. For example the following works ====== $route {$routeDest} = $cost ; print...
    4. 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 = {...
    5. 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...
  3. #2

    Default Re: hash of hashes

    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: [email]paul@jabber.rtelekom.ru[/email]
    RIPE: PA1291-RIPE


    Paul Argentoff Guest

  4. #3

    Default Re: hash of hashes

    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>



    Emmanuel Touzery Guest

  5. #4

    Default Re: hash of hashes

    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: [email]paul@jabber.rtelekom.ru[/email]
    RIPE: PA1291-RIPE


    Paul Argentoff Guest

  6. #5

    Default Re: hash of hashes

    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%




    Emmanuel Touzery Guest

  7. #6

    Default Re: hash of hashes

    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: [email]paul@jabber.rtelekom.ru[/email]
    RIPE: PA1291-RIPE


    Paul Argentoff Guest

  8. #7

    Default Re: hash of hashes

    On Fri, 3 Oct 2003 20:17:50 +0900, Paul Argentoff wrote:
    > On Friday 03 October 2003 14:48, Emmanuel Touzery wrote:
    >> not sure i understand your question. does this help you?
    > Here is my example:
    > irb(main):008:0> a = Hash.new(Hash.new(Array.new(2,0)))
    This won't work because you're not doing what you're expecting. What
    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

  9. #8

    Default Re: hash of hashes

    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}}}}}


    Joel VanderWerf Guest

  10. #9

    Default 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

  11. #10

    Default Re: hash of hashes

    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.


    Joel VanderWerf Guest

  12. #11

    Default 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,
    > irb(main):001:0> a=Hash.new({})
    > => {}
    > irb(main):002:0> a['foo']
    > => {}
    I don't think that's what you want:

    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

  13. #12

    Default Re: hash of hashes

    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...


    Joel VanderWerf Guest

  14. #13

    Default Re: hash of hashes

    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.

    martin
    Martin DeMello Guest

  15. #14

    Default 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

  16. #15

    Default Re: hash of hashes


    "Paul Argentoff" <argentoff@rtelekom.ru> schrieb im Newsbeitrag
    news:200310031517.45708.argentoff@rtelekom.ru...
    > 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
    That's because the form Hash.new( obj ) just replaces the nil return value
    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

  17. #16

    Default Re: hash of hashes


    "Joel VanderWerf" <vjoel@PATH.Berkeley.EDU> schrieb im Newsbeitrag
    news:3F7E3AF2.2050701@path.berkeley.edu...
    > 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...
    Not really:

    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

  18. #17

    Default Re: hash of hashes

    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 yourself.


    Joel VanderWerf Guest

  19. #18

    Default Re: hash of hashes


    "Joel VanderWerf" <vjoel@PATH.Berkeley.EDU> schrieb im Newsbeitrag
    news:3F7F0FF8.1030708@path.berkeley.edu...
    > 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
    yourself.

    .... and it's always the same. Something not wanted in this case.

    robert

    Robert Klemme Guest

  20. #19

    Default 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

  21. #20

    Default 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]
    Clement.laurencia is offline Junior Member
    Join Date
    Mar 2012
    Posts
    9

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139