[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

hash, key, problem...

psy

6/18/2008 9:40:00 AM

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 help me?
Thanks in advance.

Katja

4 Answers

Robert Klemme

6/18/2008 9:46:00 AM

0

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

Srijayanth Sridhar

6/18/2008 9:50:00 AM

0

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

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 help me?
> Thanks in advance.
>
> Katja
>
>

psy

6/18/2008 10:00:00 AM

0

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




psy

6/18/2008 10:04:00 AM

0

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