I needed to plot distribution of timed events today and ended up with the following Ruby script to prepare the csv data:
#!/usr/bin/env ruby # # (c) 2009 Vlad Didenko # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # See the GNU General Public License at # http://www.gnu.org/licenses/gpl-3.0.txt # for the detailed text of the license. # data = {} while gets() do $_.split().each do |item| inum = item.to_i if data[inum]; then data[inum] += 1; else data[inum] = 1 end end end data.keys().sort().each do |key| puts "#{key}, #{data[key]}" end
Here is the usage example when this script saved as distr
executable:
$ echo "1 4 235 3 2 3 1 4 2 3 4 1 2 45 6 3 2" | distr 1, 3 2, 4 3, 4 4, 3 6, 1 45, 1 235, 1 $
As with any gets
-based script, it takes filenames as parameters as well.
No comments :
Post a Comment
Comments which in my opinion do not contribute to a discussion will be removed.