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

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

android - How to create dynamically Fragment pager adapter -

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