Tuesday, August 20, 2013

Re: Basic calculation in Vim

On Tue, Aug 20, 2013 at 09:16:20AM -0700, 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 ?

I am of the opinion, and I freely admit it's my own bias, that it is
redundant to store calculated fields in data. If your feuillet page
is always NbCar / 1500 I see no reason to store it. Simply calcluate
it on the fly when you display it.

As an example, I keep gas mileage statistics for my motorcycle. All I
store are the date, mileage, miles on tank (my mileage odometer
doesn't have tenths, so I use trip odometer A for gasoline purchases),
number of gallons purchased, and the miles remaining in tank reading
from the bike's computer (I like to test its accuracy - I find this
highly entertaining). Then when I display the data I use cat and pipe
the output through awk (with a lot of other bells and whistles) and
let awk calculate my mpg and actual miles remaining in tank before it
shows it all to me.

It's a simple awk because my data is reliably static, so I can show it
here:

<awk>
{ /* print */
dt = $1
mileage = $2
miles = $3
gal = $4
rem = $5
arem = (6 - gal) * (miles / gal)
if (length(miles) > 0 && length(gal) > 0) {
if (length(rem) > 0) {
printf "%10s %8s %7s %7s %6.1f %5.0f %5d %6.1f\n", dt, mileage, miles, gal, miles / gal, miles * 6.0 / gal, rem, arem
} else {
printf "%10s %8s %7s %7s %6.1f %5.0f\n", dt, mileage, miles, gal, miles / gal, miles * 6.0 / gal
}
} else {
print
}
}
</awk>

with apologies for mailer wrap.

You could easily modify this to your purpose...

--
_|_ _ __|_|_ ._ o|
|_(_)(_)|_| ||_)||<
|

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