Kodama's home / tips.

Japanese description.

How to get the top n-data

sized_pqueue.rb : Class to get the top n-data.

PQueue or priority queue for ruby is also needed.

To see statistics, use ruby Histogram class.

Example: Get top data

require "sized_pqueue"
t=SizedPQueue.new(5,lambda{|x,y| y>x}) # top 5-data. Set reversed order.
[1,2,7,8,3,5,4,9,10,5,6,8].each{|v| t.push(v)} # scan data
p t.to_a.join(",") # print out

Result:

$ ruby sample.rb
"7,8,8,9,10"

Example: Get longest lines in a text file

require "sized_pqueue"
t=SizedPQueue.new(3,lambda{|x,y| y.size>x.size}) # top 3 lines of size
readlines.each{|l| t.push(l)}  # scan lines
t.each_pop{|l| print l} # print out

Result: case of the file "expect.rb"

$ ruby sample.rb < /usr/local/lib/ruby/1.8/expect.rb
        result = [buf,*mat.to_a[1..-1]]
      e_pat = Regexp.new(Regexp.quote(pat))
      if IO.select([self],nil,nil,timeout).nil? then

Followings are Japanese descriptions.

上位 n 個を求めろ

sized_pqueue.rb : データ列のうち上位 n 個を抽出するクラス.

priority queue for ruby が必要.

原さん(Shin-ichiro HARA) の ruby-list への投稿を見ると, 始めに自分で試した方法よりもすっきりした解決をしていた. ...ということで, 利用させていただきます.

分布,統計を見るには ruby ヒストグラム クラス を使う.


Kodama's home / tips.