From: Bertram Scharpf Date: 2009-02-13T21:51:40+09:00 Subject: Re: How to retrive first 10 items from hash in ruby? Hi, Am Freitag, 13. Feb 2009, 21:08:19 +0900 schrieb Dave Baldwin: > > On 13 Feb 2009, at 11:04, Vikas Gholap wrote: > >> I want to retrieve first 10 items(key value pairs) from given hash{} >> >> hashTable.sort {|a,b| -1*(a[1]<=>b[1])} > > You need to save the results. > ary = hashTable.sort {|a,b| -1*(a[1]<=>b[1])} > > ary[0, 10] will return an array of the first 10 entries > > ary[0, 10].each {|e| puts "Key: #{e[0} ==> #{[e[1]}" As ary is only of temporary use, you don't need to create another array ary[0,10]. I suggest: 10.times { puts "Key: %s ==> %s" % e.shift } e.clear # optional Bertram -- Bertram Scharpf Stuttgart, Deutschland/Germany http://www.bertram-scharpf.de