#!/usr/bin/ruby #バイト列の頻度を求める table = {} while !ARGV.empty? fname = ARGV.shift begin open(fname, "r"){|io| $stderr.puts fname io.binmode c = io.read(1) while !io.eof? c2 = io.read(1) num = ("#{c}#{c2}".unpack("v").first >> 4) if table.include?(num) then table[num] += 1 else table[num] = 1 end c = c2 end } rescue end sorted_table = table.to_a.sort{|a, b| a.last <=> b.last} sorted_table[0..10].each{|a, b| puts sprintf("| %03xx | #{b} |", a) } puts "=======" sorted_table[-10..-1].each{|a, b| puts sprintf("| %03xx | #{b} |", a) } end