Skip to content

Instantly share code, notes, and snippets.

@jrsconfitto
Created June 9, 2011 15:18
Show Gist options
  • Save jrsconfitto/1016944 to your computer and use it in GitHub Desktop.
Save jrsconfitto/1016944 to your computer and use it in GitHub Desktop.
Alphabetizes the words in a file and then prints them line by line. There will be a line for each first character in each word.
#! ruby
# This script will take the words in a file or passed argument and alphabetize them. Then they will be put out with a line for each starting letter.
# This will help me with grabbing all the keywords and organzing them alphabetically by line so that in the future adding keywords to the Edict language spec will be easier since they are in an alphabetical list.
if ARGV.size == 1
lines = []
input = ""
output = ""
if File.exists?(ARGV.first)
input = File.open(ARGV.first, "r").read
else
input = ARGV.first
end
# Moves through each line adding each word to an array of words
werds = input.scan(/\w+/).sort.uniq
startingLetter = werds[0][0]
werds.each do |werd|
if startingLetter != werd[0]
lines << output
startingLetter = werd[0]
output = ""
end
output += werd + " "
end
puts lines
else
puts "This script will take the words in a file or passed argument and alphabetize them. Then they will be put out with a line for each starting letter."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment