2007/10/30, Robert Keller <rlkeller / yahoo.com>:
>
>
> I am just learning RUBY and am very impressed, I one week I've built tree routines that will save my group 6 to 7 hours each week, and they run in 2 minutes. Pretty cool. I would appreciate a suggest on how to do some. I have a script that correctly parses a directory full of text file and extracts key data. The data lines look like this:
>
> " category    value"
>
> I have 15 categories and the code below works. But is crude. I create 15 global arrays and match each line "category" to the text trying in the case statement. Would hash or an object work better. Just looking for pointer of which way to direct my research.  Thanks!

Here is a different approach. I start by creating a statistics object:

Statistics = Struct.new :count, :sum

stats = Hash.new {|h,k| h[k] = Statistics.new(0, 0)}

File.foreach "foo.dat" do |line|
  if /^\s+\b(.+?)\b\s+(\d+\.\d+)/ =~ line
    s = stats[$1]
    s.count += 1
    s.sum += Float($2)
  end
end

Note, you might have to tweak the regexp.

Kind regards

robert

-- 
use.inject do |as, often| as.you_can - without end