[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

printing 2 dimensionl hash problem

Krekna Mektek

1/18/2007 7:47:00 AM

Hi,

busy with my script, I am trying to print my hash, which is built up as follows:

hash:

key = A, value = [1,2,3]
key = B, value = [4,5,6]

etc.

I try this:

@h.each do |key,value|
print key, " is ", value.each {|x| print x, " -- " }, "\n"

The print is strange, becuase it looks like this now:

1 -- 2 -- 3 -- A is 123
4 -- 5 -- 6 -- B is 456

I don't understand why. If I interpreted this correctly, I've got the
following two questions:

1. How come the code block is executed first
2. After that the key and value is printed, however, I don't see the
print for the values here (except for the print in de the code block,
which was executed already).


Krekna

2 Answers

benjohn

1/18/2007 9:00:00 AM

0

> Hi,
>
> busy with my script, I am trying to print my hash, which is built up as
> follows:
>
> hash:
>
> key = A, value = [1,2,3]
> key = B, value = [4,5,6]
>
> etc.
>
> I try this:
>
> @h.each do |key,value|
> print key, " is ", value.each {|x| print x, " -- " }, "\n"
>
> The print is strange, becuase it looks like this now:
>
> 1 -- 2 -- 3 -- A is 123
> 4 -- 5 -- 6 -- B is 456
>
> I don't understand why. If I interpreted this correctly, I've got the
> following two questions:
>
> 1. How come the code block is executed first
> 2. After that the key and value is printed, however, I don't see the
> print for the values here (except for the print in de the code block,
> which was executed already).

:) I think what you want to actually do is:

@h.each do |key,value|
print key, " is ", value.join(' -- '), "\n"
end

or

@h.each do |key,value|
puts key, " is ", value.join(' -- ')
end

To hopefully answer your questions:

You have asked Ruby to print out the return value of value.each{...},
which is simply value:

[1,2,3] == [1,2,3].each {|n| do_something_with_n_if_you_like(n)}

So that explains the 123 at the end of the line, because [1,2,3].to_s is
"123"

Why the "1 -- 2 -- 3" before everything else though? Well, _before_ Ruby
can execute that first "print", it must _first_ evaluate what is being
printed, just as if you had writen "print 1+2+3". In calculating that,
Ruby calls values.each {...}, and this prints out the "1 -- 2 -- 3" at
the start of the line.

Hope that helps.



Krekna Mektek

1/18/2007 10:53:00 AM

0

Thank you very much, my answer was posted before I read this.
I think it's clear to me now.
That's the best way, learning by doing and talking about it what
actually happens when something went wrong.

Thank you too.

Krekna

2007/1/18, benjohn@fysh.org <benjohn@fysh.org>:
> > Hi,
> >
> > busy with my script, I am trying to print my hash, which is built up as
> > follows:
> >
> > hash:
> >
> > key = A, value = [1,2,3]
> > key = B, value = [4,5,6]
> >
> > etc.
> >
> > I try this:
> >
> > @h.each do |key,value|
> > print key, " is ", value.each {|x| print x, " -- " }, "\n"
> >
> > The print is strange, becuase it looks like this now:
> >
> > 1 -- 2 -- 3 -- A is 123
> > 4 -- 5 -- 6 -- B is 456
> >
> > I don't understand why. If I interpreted this correctly, I've got the
> > following two questions:
> >
> > 1. How come the code block is executed first
> > 2. After that the key and value is printed, however, I don't see the
> > print for the values here (except for the print in de the code block,
> > which was executed already).
>
> :) I think what you want to actually do is:
>
> @h.each do |key,value|
> print key, " is ", value.join(' -- '), "\n"
> end
>
> or
>
> @h.each do |key,value|
> puts key, " is ", value.join(' -- ')
> end
>
> To hopefully answer your questions:
>
> You have asked Ruby to print out the return value of value.each{...},
> which is simply value:
>
> [1,2,3] == [1,2,3].each {|n| do_something_with_n_if_you_like(n)}
>
> So that explains the 123 at the end of the line, because [1,2,3].to_s is
> "123"
>
> Why the "1 -- 2 -- 3" before everything else though? Well, _before_ Ruby
> can execute that first "print", it must _first_ evaluate what is being
> printed, just as if you had writen "print 1+2+3". In calculating that,
> Ruby calls values.each {...}, and this prints out the "1 -- 2 -- 3" at
> the start of the line.
>
> Hope that helps.
>
>
>
>