Accessing a hash in a hash

Discussion in 'Ruby' started by Justin To, Jun 5, 2008.

  1. Justin To

    Justin To Guest

    Hello, I'm new to Ruby and am trying this example problem. I'm suppose
    to have a hash key whose value is a hash.
    Ex. hash['a'] => hash

    Then, I'm suppose to change the second hash's keys and values...

    how do i go about this?

    Hash1: Key => ( Hash2: aKey => aValue )

    I want to change aKey and aValue

    Hash1[key]??????

    How do I index it, or access it??

    Thanks in advance!!!!
    --
    Posted via http://www.ruby-forum.com/.
     
    Justin To, Jun 5, 2008
    #1
    1. Advertising

  2. Justin To

    Justin To Guest

    Justin To, Jun 5, 2008
    #2
    1. Advertising

  3. Justin To

    Damjan Rems Guest

    Damjan Rems, Jun 5, 2008
    #3
  4. Justin To wrote:
    >
    > Hello, I'm new to Ruby and am trying this example problem. I'm suppose
    > to have a hash key whose value is a hash.
    > Ex. hash['a'] => hash
    >
    > Then, I'm suppose to change the second hash's keys and values...
    >
    > how do i go about this?
    >
    > Hash1: Key => ( Hash2: aKey => aValue )
    >
    > I want to change aKey and aValue
    >
    > Hash1[key]??????
    >
    > How do I index it, or access it??
    >
    > Thanks in advance!!!!

    ---> a["b"]["c"]=2

    D:>irb
    irb(main):001:0> a={}
    => {}
    irb(main):002:0> a["b"]={"c" => "1"}
    => {"c"=>"1"}
    irb(main):004:0> require 'pp'
    => true
    irb(main):005:0> pp a
    {"b"=>{"c"=>1}}
    => nil
    irb(main):006:0> a["b"]["c"]=2
    => 2
    irb(main):007:0> pp a
    {"b"=>{"c"=>2}}
    => nil
    --
    Posted via http://www.ruby-forum.com/.
     
    Rodrigo Bermejo, Jun 5, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    539
    red floyd
    Nov 10, 2011
  2. Chris
    Replies:
    12
    Views:
    167
    Brian Candler
    Sep 18, 2004
  3. Srijayanth Sridhar
    Replies:
    19
    Views:
    627
    David A. Black
    Jul 2, 2008
  4. Lynn

    Accessing Hash of hash of arrays

    Lynn, Feb 14, 2005, in forum: Perl Misc
    Replies:
    9
    Views:
    549
    Alan J. Flavell
    Feb 16, 2005
  5. Paul Lalli

    accessing hash values with %hash->{foo} ?

    Paul Lalli, May 31, 2006, in forum: Perl Misc
    Replies:
    2
    Views:
    105
    Brian McCauley
    May 31, 2006
Loading...

Share This Page