The bigsort.pl script on page 150 of Ruby Programming for Medicine and Biology, has a few
quirks. First, it assumes that the text file (to be sorted) is a DOS-style
file with a two character (carriage-return,line-feed) linebreak. Also, it
assumes that every line (in the file to be sorted) contains alphanumeric text.
This slightly modified Ruby script will work in any type of text file and does
not require text to appear on any lines of the file.
#!/usr/local/bin/ruby
text = File.open("terms.txt", "r")
out = File.open("terms.put", "w")
linearray = Array.new
begin_position = 0
text.each_line do
|line|
old_position = begin_position
begin_position = text.pos
line = line.chomp! + " "
linearray << line.slice(0..9) + old_position.to_s
end
linearray.sort!
linearray.each do
|value|
seekplace = value.slice(10..20).to_i
text.seek(seekplace, IO::SEEK_SET)
out.puts(text.readline)
end
exit
Thanks goes to Dr. Tim Rand, who spotted the error and sent me his own
version of a fix on February 6, 2008.