PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Problem accessing hash table
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Problem accessing hash table

Réponse
 
LinkBack Outils de la discussion
Vieux 26/05/2008, 22h17   #1
Anukul Singhal
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Problem accessing hash table

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 as to how I can access the value of the hash table
defined in the array?

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

  Réponse avec citation
Vieux 26/05/2008, 22h56   #2
Todd Benson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problem accessing hash table

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

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h14.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09882 seconds with 10 queries