Hi
On Fr, 19 Mär 2010, Stahlman Family wrote:
> epanda wrote:
>> Hi,
>>
>> I have some lines like that
>>
>>
>> foo 0.5
>> foobar 1
>> bar 0.5
>>
>>
>> Is it possible to sum which result as 2 by using regexp and writing
>> the result line below all lines checked ?
>>
>>
>> foo 0.5
>> foobar 1
>> bar 0.5
>>
>> total 2
>
> You could do it all with Vim script, but in my opinion, the best way to
> do something like that is to use a tool such as awk, in conjunction with
> Vim's filter mechanism. E.g., Visually select the original 3 lines, then
> execute...
> '<,'>!awk '{sum += $2; print} END {printf "total\t\%f", sum}'
Personally, I would also use awk.
But here is a vim script version:
fu! <sid>Sum(colnr) range
let lines=getline(a:firstline, a:lastline)
let lines=map(lines, 'split(v:val)[a:colnr-1]')
call append(a:lastline, printf("total\t%02d", eval(join(lines,'+'))))
unlet lines
endfu
com! -nargs=1 -range SumCol :<line1>,<line2>call <sid>Sum(<args>)
regards,
Christian
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
To unsubscribe from this group, send email to
vim_use+unsubscribegooglegroups.com or reply to this email with the words
"REMOVE ME" as the subject.