Skip to content

Instantly share code, notes, and snippets.

@zonuexe
Created September 27, 2011 09:30
Show Gist options
  • Save zonuexe/1244681 to your computer and use it in GitHub Desktop.
Save zonuexe/1244681 to your computer and use it in GitHub Desktop.
集計用
#!/usr/bin/env ruby
cfile_path = ARGV[0]
headnum = Multiset.new
otherec = Multiset.new
certfile = File.open(cfile_path)
linenum = 0
certfile.each_line{|line|
linenum += 1
l = line.split("\t")
n = l.size
if l[0] =~ /\d{10}/
headnum << n
else
otherec << n
end
}
puts "\n[/\\d{10}/ mached] #{headnum.listview}"
puts "\n[not matched] #{otherec.listview}"
BEGIN{
class Multiset
def initialize
@collection = Hash.new
end
def << arg
key = arg
unless @collection[key]
@collection[key] = 1
else
@collection[key] += 1
end
end
def inspect
@collection
end
def listview(order=:desc)
array = []
@collection.each{|key, value|
array << [key,value]
}
sort_func = case order
when :desc
->(a,b){ a[0] <=> b[0] }
when :asc
->(a,b){ b[0] <=> a[0] }
end
array.sort(&sort_func).inject("\n"){|str, obj|
str << ("%3d\t%5d\n" % obj )
}
end
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment