Greg recently offered a challenge: Python Golf. The idea, to create a parser for Apache's Common Log Format, to aggregate the amount of data sent to each IP address. The challenge being to do it with the least amount of characters. Soon after this challenge was issued, Sam and Greg were both convinced (before even talking to me) that I would attempt a Ruby solution. How could I dissapoint them? :) .

I tried a number of things, however I eventually settled on this solution (63 chars):

m=Hash.new(0);STDIN.map{|l|m[l[/^\\S+/]]+=l[/\\d+\\s+$/].to_i};p m


A slightly longer version, with a slightly prettier printing can be accomplished with inject (78 chars):

STDIN.inject(Hash.new(0)){|m,l|m[l[/^\\S+/]]+=l[/\\d+\\s+$/].to_i;m}.each{|v|p v}


Not to brag, but the winning Python entry was 121 characters. So the Ruby solution managed to be almost half this count!