Created
August 23, 2011 14:43
-
-
Save jdkealy/1165305 to your computer and use it in GitHub Desktop.
This file contains 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
class Output < | |
Struct.new(:name, :price) | |
end | |
unless ARGV.length == 1 | |
puts "Wrong number of Arguments" | |
puts "Usage: ruby #{File.basename(__FILE__)} InputFile.tsv > SortedOutputFile.tsv\n" | |
exit | |
end | |
# get the input filename from the command line | |
input_file = ARGV[0] | |
# define an array to hold the Output records | |
arr = Array.new | |
#loop through tsv, add to array | |
f = File.open(input_file, "r") | |
f.each_with_index { |line, i| | |
name_index = "" | |
price_index = "" | |
if(i==0) then | |
headers = line.split("\t") | |
headers[2] = headers.last.strip | |
name_index = headers.index("Name") | |
price_index = headers.index("Price") | |
end | |
next if i == 0 | |
next if line[0] == "#" | |
words = line.split("\t") | |
p = Output.new | |
puts name_index | |
p.name = words[0] | |
p.price = words[2].to_f | |
arr.push(p) | |
} | |
# sort the data by the price field | |
arr.sort! { |a,b| a.price <=> b.price } | |
# print out all the sorted records | |
puts "Name" + "\t" + "Price" | |
arr.each { |p| | |
puts p[0] + "\t" + p[1].to_s + "\n" | |
} | |
~ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment