From: Ara.T.Howard@... Date: 2004-11-10T01:53:34+09:00 Subject: Re: Iterating trough hash --8323328-2051310531-1100018488=:11896 Content-Type: MULTIPART/MIXED; BOUNDARY="8323328-2051310531-1100018488=:11896" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --8323328-2051310531-1100018488=:11896 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE On Tue, 9 Nov 2004, Kevin [ISO-8859-15] B=F6rgens wrote: > Hi! > > This is my first ruby day. I started reading the ruby book two hours ago = and > want to do something useful now :-) > I have a hash and I want to iterate trough all pairs of values. An exampl= e: > > h =3D {"john" =3D> 41, "mary" =3D> 31, "fred" =3D> 10} > #insert control structure here > age=3D(h[person1]+h[person2]).to_s > puts "#person1 shakes hands with #person2. Together they are #age > years old" > > > The output could for example be: > > john shakes hands with mary. Together they are 72 years old > john shakes hands with fred. Together they are 51 years old > mary shakes hands with fred. Together they are 41 years old > > > what I do at the moment: > > h =3D {"john" =3D> 41, "mary" =3D> 31, "fred" =3D> 10} > h.each_key{|person1| > h.each_key{|person2| > if h.sort.index([person1,h[person1]]) age=3D(h[person1]+h[person2]).to_s > puts "#{person1} shakes hands with #{person2}. Together they are > #{age} years old" > end }} > > Is there a more elegant way to do this? > > TIA, > Kevin not sure if it's more elegant, but harp:~ > cat a.rb h =3D {"john" =3D> 41, "mary" =3D> 31, "fred" =3D> 10} sorted =3D h.sort sorted.each do |this| greater_than =3D h.select{|*that| (that <=3D> this) > 0} greater_than.each do |that| p0, a0, p1, a1 =3D this + that puts "#{ p0 } shakes hands with #{ p1 }. Together they are #{ a0 + a= 1 } years old" end end harp:~ > ruby a.rb fred shakes hands with john. Together they are 51 years old fred shakes hands with mary. Together they are 41 years old john shakes hands with mary. Together they are 72 years old is, i think, closer to what you want. note that with your code the order o= f output is undefined : keys come out of a hash in an undefined order. load = your hash with hundred entries and run a few times to see what i mean... kind regards. -a -- =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | When you do something, you should burn yourself completely, like a good | bonfire, leaving no trace of yourself. --Shunryu Suzuki =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D --8323328-2051310531-1100018488=:11896-- --8323328-2051310531-1100018488=:11896--