Skip to content

Instantly share code, notes, and snippets.

@znbailey
Created February 7, 2011 21:54
Show Gist options
  • Save znbailey/815317 to your computer and use it in GitHub Desktop.
Save znbailey/815317 to your computer and use it in GitHub Desktop.
Outputs the n shortest lines from a file
num_longest = ARGV[1].to_i
longest_lines = []
shortest_len = -1
infile = File.new(ARGV[0], "r")
while ( line = infile.gets )
unless line.length > shortest_len
continue
end
longest_lines.push(line)
longest_lines = longest_lines.sort {|a,b| b.length <=> a.length }
if longest_lines.length > num_longest
longest_lines.pop
end
shortest_len = longest_lines[-1].length
end
longest_lines.each do |l|
puts l
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment