From: Ara.T.Howard@... Date: 2004-11-10T02:03:45+09:00 Subject: Re: Iterating trough hash --8323328-1218805945-1100019027=:11896 Content-Type: MULTIPART/MIXED; BOUNDARY="8323328-1218805945-1100019027=: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-1218805945-1100019027=:11896 Content-Type: TEXT/PLAIN; CHARSET=X-UNKNOWN; FORMAT=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Content-ID: On Tue, 9 Nov 2004, Kevin [ISO-8859-15] B=F6rgens wrote: > Hi! >> #!/usr/bin/env ruby >> >> h =3D {"john" =3D> 41, "mary" =3D> 31, "fred" =3D> 10} >> h.each_key do |person1| >> h.each_key do |person2| >> puts "#{person1} shakes hands with #{person2}. " + >> "Together they are #{h[person1] + h[person2]} years old." >> end >> end > No, this programm would iterate like this > john john > john mary > john fred > mary john > mary mary > mary fred > fred john > fred mary > fred fred > >> I'm not very clear on what you were aiming for with the if statement, >> but if you tell me, I can probably add that back a little slimmer too. > Therefore I used the example with shaking hands. You don't shake hands wi= th > yourself and it doesn't matter who is person1 or person2 > > TIA, > Kevin that makes sense - but why, in your code, are you checking that the person'= s name AND/OR age are less than the other person? eg this line if h.sort.index([person1,h[person1]]) < h.sort.index([person2,h[person2]= ]) says: if the name of person1 is less than the name of person2 OR the names are= the SAME but the age of person1 is less than the age of person2. example: harp:~ > irb irb(main):001:0> list =3D ['john', 42], ['john', 41] =3D> [["john", 42], ["john", 41]] irb(main):002:0> a, b =3D list.first, list.last =3D> [["john", 42], ["john", 41]] irb(main):003:0> list.index(a) =3D> 0 irb(main):004:0> list.index(b) =3D> 1 irb(main):005:0> list.index(a) < list.index(b) =3D> true and john is shaking hands with john. ruby sorts array by using this algori= thim: compare the first elements, if tied compare the second elements, if tied compare the third elelments, etc.... etc.... so the comparison you are making by checking the sort order (index position= of entry) checks both name AND age - which may or may not be what you want. cheers and welcome to ruby! -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-1218805945-1100019027=:11896-- --8323328-1218805945-1100019027=:11896--