Ross Bamford
5/18/2006 3:10:00 PM
On Thu, 18 May 2006 15:51:26 +0100, Sammyo <samuel.murphy@gmail.com> wrote:
> Is there a cleaner way to do this?
>
> $ cat example_data
> 1 foo 3943
> 2 bar 8989
> 3 baz 9088
>
> $ ruby -e 'i=0;ARGF.each{|l| i += l.split[2].to_i};p i' example_data
> 22020
>
> As a relative newby I tried inject which seemed cool but
> converting from string to int seem defeated that approach.
>
How about:
$ ruby -e 'p ARGF.inject(0){|a,b|a+b.split[2].to_i}' example_data
22020
--
Ross Bamford - rosco@roscopeco.remove.co.uk