From: Charlotte Date: 2006-04-12T23:52:33+09:00 Subject: First script seems slow - What's a better way to write this? I've inherited a tcl script from previous co-op students, and it's a little messy so I wanted to clean it up. I wanted to learn Ruby anyway, so I made a ruby script to search my .tcl file and output a list of all the procedures and variables, sorted in order of number of times of use (I'm mainly interested in the unused ones). The script seems really slow though (~10 seconds for a 3000 line file)- is that Ruby, or is it just my implementation? I don't care that this script takes 10 seconds, but I'd like to learn how to write better ruby code. Here's my script: def generateTokenList(readFile, token, prefix) names = Hash.new str = "" File.open(readFile, 'r').each do |line| if line[token] and not line['#'] name = line.split[1] names[name] = 0 if not names.key?(name) end end names.each do |key, value| i = 0 i = -1 if token == 'proc ' File.open(readFile, 'r').each do |line| i = i + 1 if line[prefix + key] and not line['#'] end names[key] = i end names = names.sort { |a,b| a[1] <=>b [1] } names.each { |pair| str << pair[0] + " uses: " + pair[1].to_s + "\n" } return str end if ARGV[0] == nil or ARGV[1] == nil puts "\nUsage: ruby ProcList.rb inputfilepath outputfilename" exit(0) end writeFile = File.new(ARGV[1], 'w') writeFile << "Procedures:\n" writeFile << generateTokenList(ARGV[0], 'proc ', '') writeFile << "\n\nVariables:\n" writeFile << generateTokenList(ARGV[0], 'set ', 36.chr) writeFile << "Updated: " + File.mtime(ARGV[0]).to_s -- Posted via http://www.ruby-forum.com/.