From: James Edward Gray II Date: 2006-12-16T09:44:08+09:00 Subject: Re: matching lines.... On Dec 15, 2006, at 6:12 PM, Just Maz wrote: > I've got a file with a few lines: > 1: Kevin 25 football cricket guitar > 2: John 15 football karate > 3: Fred 20 rugby painting > > Via regular expressions I'm trying to do a few things here such as > counting how many hobbies people have and counting how many people > have > the same hobby- its this last one in particular I can't figure out. See if this gives you some fresh ideas: persons_hobbies = Hash.new hobbies = Hash.new DATA.each do |line| fields = line.split fields[3..-1].each do |hobby| (persons_hobbies[fields[1]] ||= Array.new) << hobby (hobbies[hobby] ||= Array.new) << fields[1] end end puts "=== Hobby Counts ===" puts hobbies.map { |h, c| "#{h}: #{c.size}" } puts "=== Personal Counts ===" puts persons_hobbies.map { |n, c| "#{n}: #{c.size}" } __END__ 1: Kevin 25 football cricket guitar 2: John 15 football karate 3: Fred 20 rugby painting Hope that helps. James Edward Gray II