From: Brad Winborg Date: 2009-03-02T13:40:16+09:00 Subject: Re: Error when running script Phlip wrote: > Brad Winborg wrote: > >> I did check the case and it was wrong. I changed it to match and it made >> a difference. I still don't think its working correctly. If your curious >> I have attached the text file that it is reading. >> >> my_path = "C:\\Ruby\\Text\\" > > Paths are much easier to manage using / , even on Windows. > >> WARNING = 0 >> ERROR = 0 > > These are not the same as warning and error. And this... > > warnings = 0 > errors = 0 > > ...provides better "ear" - better hints that we are counting things. > >> d = Dir.new(my_path) >> >> d.each do |fl| >> unless fl == '.' or fl == '..' then > > Only use unless in a pinch. This is better style: > > if fl != '.' and fl != '..' > >> puts my_path + fl >> File.open(my_path + fl, "r").each do |line| > > Try File.readlines(my_path + fl).each do |line| > >> if line =~ /ERROR/ then >> error += 1 > > You don't need 'then'. It's allowed, but it makes the code more verbose. > >> elsif line =~ /WARNING/ then >> warning += 1 >> end >> end >> >> end >> end >> puts warning.to_s >> puts error.to_s > > puts implies .to_s. Try: > > puts "Warnings: #{warnings}; Errors: #{errors}" > > Now, would this work? > > warnings = File.readlines(my_path + fl).grep(/WARNING/).length > > You would be amazed what Ruby can pack into one statement! I am feeling a bit stupid at this point parts of what you have said seem to make sense but other parts don't. I don't to keep bothering you but I really want to understand what is happening and I am not sure what you are telling me to replace. You mention warnings allot but not error. I have replaced what I believe you were saying. whatever you have been a tremendous help and I hope one gay I can return the favor, but all this is new stuff to me. I have read allot and it has not helped so far that is why I have turned to forums like this because I can actually interact with real people. Below is the code I changed to what I thought you were telling me. my_path = "C:/Ruby/Text/" warnings = 0 errors = 0 d = Dir.new(my_path) if fl != '.' and fl != '..' File.readlines(my_path + fl).each do |line| if line =~ /ERROR/ error += 1 elsif line =~ /WARNING/ warning += 1 end end end end puts implies .to_s. Try: puts "Warnings: #{warnings}; Errors: #{errors}" warnings = File.readlines(my_path + fl).grep(/WARNING/).length puts error.to_s -- Posted via http://www.ruby-forum.com/.