Tuesday, August 20, 2013

Re: Basic calculation in Vim

On Di, 20 Aug 2013, tjg wrote:

> I have written a small function which puts "WIP statistics" at the end of the
> file (pure text, no code) I am working on.
>
> It looks like this (ts = 7)
>
> Date NbCar NbWords NbSent NbLines
> 130813 21910 3640 310 180
> 130820 30310 5210 480 220
>
> (NB : Date in the ymd format, Nb=number, Car=Characters, Sent=Sentences
> (separated by .!?…) , and Lines are, of course, non-blank lines and, thus,
> the equivalent of book paragraphs).
>
> This function works. But I would like to add 2 "columns" :
>
> - one about the final output : divide the NbCar by 1500 (in France a
> journalistic "feuillet"/page, I do not know if there is an equivalent
> elsewhere) ; here it would indicate that a week ago I had written 15
> feuillets (rounded upwards), and this week, 20 feuillets : a 250 pages book
> in a year, "In search of lost time" much later, genius not included…
>
> - one about simple readability : divide the number of words by the number of
> sentences.
>
> How should I proceed ?
>
> For the first one (output) I tried to move the cursor on the number (e.g.
> 30310), enter insert mode then <Ctrl-R>= followed by <Ctrl-R><Ctrl-W>/1500,
> but failed miserably.
>
> As for the second one (readability), I simply cannot figure it out.
>
> Thanks in advance

I just committed an update to the csv filetype plugin¹

Now you can do this:

1) [Append a column that is the result of column 2/1500]
:2,$s#$#\=printf("%.2f", (CSVField(2,line('.'))+0.0)/1500)#

This is one single command and it uses some expression evaluation of the
:s command to perform the calculation
(Note: the useage of # instead of the usual '/' delimiter, since we need
the / to divide the numbers, note also, we need to leave out the 1st row
as it does not contain numbers)

2) [Append a column that is the result of column 3/column 4]
:2,$s#$#\=printf("%.2f%s",
(CSVField(3,line('.'))+0.0)/(CSVField(4,line('.'))+0.0), b:delimiter)#

Again, entered as one line. Note the usage of the buffer local variable
"b:delimiter" which holds the actual delimiter variable (in your case a
"\t" tabulator).


See some more basics at
:h sub-replace-expression

And of course, if you have the csv filetype plugin installed:
:h ft-csv.txt
:h csv-calculate-column

Hope this helps a little. If you have questions feel free to mail me.

¹)https://github.com/chrisbra/csv.vim

regards,
Christian
--
Ihr, die ihr noch jung seid, hört einen Alten, auf den die Alten
hörten, als er noch jung war!
-- Kaiser Augustus, bei Plutarch

--
--
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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: