[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Problem accessing hash table

Anukul Singhal

5/26/2008 9:18:00 PM

Hi,

I am facing the following problem, when using the following code
snippet:

c = []
c.push({:name => 'xyz', :class => 'm70', :title => 'yes'})

Now, puts c gives me the output as classm70namexyztitleyes

My Aim is to access the :name attribute of the hash table that is being
pushed into the c array. I tried the following:

puts c[:name] , gives an error - p046hashes.rb:10:in `[]': Symbol as
array index (TypeError)
from p046hashes.rb:10

Can anyone help as to how I can access the value of the hash table
defined in the array?

Thanks,
Anukul
--
Posted via http://www.ruby-....

1 Answer

Todd Benson

5/26/2008 9:56:00 PM

0

On Mon, May 26, 2008 at 4:17 PM, Anukul Singhal
<anukul.singhal@gmail.com> wrote:
> Hi,
>
> I am facing the following problem, when using the following code
> snippet:
>
> c = []
> c.push({:name => 'xyz', :class => 'm70', :title => 'yes'})
>
> Now, puts c gives me the output as classm70namexyztitleyes
>
> My Aim is to access the :name attribute of the hash table that is being
> pushed into the c array. I tried the following:
>
> puts c[:name] , gives an error - p046hashes.rb:10:in `[]': Symbol as
> array index (TypeError)
> from p046hashes.rb:10
>
> Can anyone help as to how I can access the value of the hash table
> defined in the array?
>
> Thanks,
> Anukul
> --

In your example, you have a Hash instance as an element of an Array
instance. That doesn't sound like what you probably want. But if it
is, and since it is the first element in your array c, you can get
that hash with c[0].

puts c[0][:name]

hth,
Todd