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 > hash, key, problem...
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
hash, key, problem...

Réponse
 
LinkBack Outils de la discussion
Vieux 18/06/2008, 10h39   #1
psy
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut hash, key, problem...

Hallo people,
I cant get something done since a while. Its very basic stuff..

I have the following hash for example :
hash = { $-a => "info", $F => "another info"}
and so on, you get the meaning..
Now i use Hash#each_pair so i get the value of the variables and the
info, but I would like also to get a simple string, containing
the name of the variable, so it should look like this :
$-a
info
false

$F
another info
nil

But I cant get it done. Its probably very stupid question, but doesnt
matter what I try - i dont get the name of the key, i always get the
key itself returned... Can somebody me?
Thanks in advance.

Katja

  Réponse avec citation
Vieux 18/06/2008, 10h45   #2
Robert Klemme
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: hash, key, problem...

On 18 Jun., 11:39, psy <p...@matt-schwarz.com> wrote:
> Hallo people,
> I cant get something done since a while. Its very basic stuff..
>
> I have the following hash for example :
> hash = { $-a => "info", $F => "another info"}
> and so on, you get the meaning..
> Now i use Hash#each_pair so i get the value of the variables and the
> info, but I would like also to get a simple string, containing
> the name of the variable, so it should look like this :
> $-a
> info
> false
>
> $F
> another info
> nil
>
> But I cant get it done. Its probably very stupid question, but doesnt
> matter what I try - i dont get the name of the key, i always get the
> key itself returned... Can somebody me?
> Thanks in advance.


Then just put the names in the Hash as keys.

hash = { "$-a" => "info", "$F" => "another info"}
hash = { "$-a" => [$-a, "info"], "$F" => [$F, "another info"]}

Btw, what are you trying to accomplish?

robert
  Réponse avec citation
Vieux 18/06/2008, 10h49   #3
Srijayanth Sridhar
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: hash, key, problem...

On Wed, Jun 18, 2008 at 3:09 PM, psy <psy@matt-schwarz.com> wrote:
> Hallo people,
> I cant get something done since a while. Its very basic stuff..
>
> I have the following hash for example :
> hash = { $-a => "info", $F => "another info"}
> and so on, you get the meaning..
> Now i use Hash#each_pair so i get the value of the variables and the
> info, but I would like also to get a simple string, containing
> the name of the variable, so it should look like this :
> $-a
> info
> false
>
> $F
> another info
> nil


Do you want something like this:

hash={:name => "John Doe", :age => 23}

hash.each do |elem|
puts elem.join("\n")
end

That should work. You can also of course get the string as:

string=elem.join("\n")

Hope that s.

Jay






>
> But I cant get it done. Its probably very stupid question, but doesnt
> matter what I try - i dont get the name of the key, i always get the
> key itself returned... Can somebody me?
> Thanks in advance.
>
> Katja
>
>


  Réponse avec citation
Vieux 18/06/2008, 11h00   #4
psy
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: hash, key, problem...

On Wed, 18 Jun 2008 18:48:50 +0900
Robert Klemme <shortcutter@googlemail.com> wrote:
>
> Then just put the names in the Hash as keys.
>
> hash = { "$-a" => "info", "$F" => "another info"}
> hash = { "$-a" => [$-a, "info"], "$F" => [$F, "another info"]}
>
> Btw, what are you trying to accomplish?
>
> robert
>


*blush*
Oh, man... Thank you.
To your question : Im trying to make a small program, which prints
well formated variable-info in the console. Stupid stuff...

Katja




  Réponse avec citation
Vieux 18/06/2008, 11h04   #5
psy
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: hash, key, problem...

On Wed, 18 Jun 2008 18:49:53 +0900
"Srijayanth Sridhar" <srijayanth@gmail.com> wrote:
>
> Do you want something like this:
>
> hash={:name => "John Doe", :age => 23}
>
> hash.each do |elem|
> puts elem.join("\n")
> end
>
> That should work. You can also of course get the string as:
>
> string=elem.join("\n")
>
> Hope that s.
>
> Jay
>


Yeah, this would work, but now there is another problem - it dont prints
the value of the variables.
Btw I got it done.

Thank you anyway

Katja

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


É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 ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10664 seconds with 13 queries