ruby on rails - How to Calculate sum of all the digits in text file -


i having text file t.txt,i want calculate sum of digits in text file example

    --- t.txt --- rahul  jumped in 2 well. water cold @ 1 degree centigrade. there 3 grip holes on walls.  17 feet deep. --- eof -- 

sum 2+1+3+1+7 ruby code calculate sum is

ruby -e "file.read('t.txt').split.inject(0){|mem, obj| mem += obj.to_f}" 

but not getting answer??

your statement computing correctly. add puts before file read as:

ruby -e "puts file.read('t.txt').split.inject(0){|mem, obj| mem += obj.to_f}" # => 23.0 

for summing single digit only:

ruby -e "puts file.read('t.txt').scan(/\d/).inject(0){|mem, obj| mem += obj.to_f}" # => 14.0 

thanks


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -