Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Accessing a hash in a hash

Reply
Thread Tools

Accessing a hash in a hash

 
 
Justin To
Guest
Posts: n/a
 
      06-05-2008

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

 
Reply With Quote
 
 
 
 
Justin To
Guest
Posts: n/a
 
      06-05-2008
I figured it out
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Damjan Rems
Guest
Posts: n/a
 
      06-05-2008

Blind guess.

How about hash[key][key]


by
TheR
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Rodrigo Bermejo
Guest
Posts: n/a
 
      06-05-2008
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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
accessing hash values with %hash->{foo} ? Paul Lalli Perl Misc 2 05-31-2006 04:50 PM
Accessing Hash of hash of arrays Lynn Perl Misc 9 02-16-2005 06:59 PM
Accessing Hash elements in sorted order? Chris Ruby 12 09-18-2004 04:23 PM



Advertisments