From: Robert Klemme Date: 2008-06-06T18:04:21+09:00 Subject: Re: Histogram 2008/6/6 Justin To : > I've been looking around to learn about histograms, and haven't had any > luck... can someone tell me how a histogram would work for something > like this: > > Name, Age > Bob, 2 > Jim, 2 > Eric, 4 > Rob, 5 > > How would I use a histogram to get a result like this: > > Age | Names > 2 | Bob, Jim > 4 | Eric > 5 | Rob Here's a variant: 10:43:33 $ /c/Temp/hist.rb Age | Name 2 | Bob,Jim 4 | Eric 5 | Rob 11:03:55 $ cat /c/Temp/hist.rb #!/bin/env ruby hist = Hash.new {|h,k| h[k]=[]} DATA.each do |line| name, age = line.split /,\s*/ hist[Integer(age)] << name rescue nil end printf "%-4s | %s\n", "Age", "Name" hist.sort_by {|k,| k}.each do |k,v| printf "%4d | %s\n", k, v.join(',') end __END__ Name, Age Bob, 2 Jim, 2 Eric, 4 Rob, 5 11:04:08 $ Kind regards robert -- use.inject do |as, often| as.you_can - without end