Created
August 17, 2009 06:14
-
-
Save JohnB/168942 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=begin | |
My brother created a C++ app for parsing cal files (not calendars) | |
and outputting the final matching data as a CSV. This is my attempt | |
in Ruby. | |
Expected incoming format: some text files that match the regexp's in | |
the list (e.g. "value=123") | |
Expected output: CSV format, ordered according to the columns array. | |
=end | |
filename = "foo.cal" | |
str = IO.Read(filename) | |
#test example: | |
#str = "asdffdsdf\nsdsdfsuper=234 \n value=111 super=55 \nsuper=888 " | |
columns = [ | |
["super", /super=(\d+)\s/], | |
["dooper", /value=(\w+)\s/] | |
] | |
result = {} | |
str.split("\n").each do |line| | |
columns.each do |name_and_regexp| | |
name, regexp = name_and_regexp | |
if line =~ regexp | |
result[name] = $1 | |
end | |
end | |
end | |
headers = [] | |
csv_row = [] | |
columns.each do |name_and_regexp| | |
name = name_and_regexp.first | |
headers << name | |
csv_row << result[name] | |
end | |
puts headers.join(",") | |
puts csv_row.join(",") | |
#Example output: | |
# super,dooper | |
# 888,111 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment