Sunday, May 19, 2013

Re: How to do math in vim ?

On 19/05/13 19:00, Jeri Raye wrote:
> Hi,
>
> Can you do math in Vim?
> Can you do subtractions?

Sure you can do math, including subtractions and even much more
complicated stuff than that -- on integers or on floating-point numbers
but not on hour-minute-second times. For the latter, you have to convert
them first to integers or floating-point numbers. In your case, to
integral milliseconds (which I think would be best) or to floating-point
seconds. (There are 86400000 milliseconds in a day, and IIUC the highest
positive 32-bit signed integer is 2147483647 which leaves quite a bit of
room.)

See
:help split()
:help expr5
:help join()
:help printf()

Beware that in Vim, the decimal point is always a dot, never a comma,
regardless of locale. You may want to use substitute() (q.v.) to replace
the comma by a dot in your seconds-and-milliseconds part, or else, split
at the comma (as well as at both colons) and compute
((((((hours*60)+minutes)*60)+seconds)*1000)+milliseconds) giving you an
integral time in milliseconds. To convert the integral milliseconds back
to hours-minutes-seconds-milliseconds, see the integral / (quotient) and
% (remainder) binary operators.

>
> I have a srt file where I want to subtract 7 from all the seconds in the
> file.
> So this:
> +------------------------------------+
> 24
> 00:07:55,641 --> 00:07:58,393
> You can't do that to us!
> We'll tell you a secret.
>
> 25
> 00:07:58,603 --> 00:07:59,769
> A secret? Who by?
> +------------------------------------+
>
>
> becomes:
> +------------------------------------+
> 24
> 00:07:48,641 --> 00:07:51,393
> You can't do that to us!
> We'll tell you a secret.
>
> 25
> 00:07:51,603 --> 00:07:52,769
> A secret? Who by?
> +------------------------------------+
> The changes in this are:
> (55 becomes 48)
> (58 becomes 51)
> (59 becomes 52)
>
>
> Meaning:
> - search for the lines with -->
> - search for the number before the first , (comma)
> - subtract that number with 7
> - search for the number before the second , (comma)
> - subtract that number with 7
>
>
> I don't know if vim can subtract it also to negative numbers?
> In case you have this:
> +------------------------------------+
> 27
> 00:08:01,898 --> 00:08:04,316
> She's visiting--
>
> 28
> 00:08:04,525 --> 00:08:07,277
> Who wants to know about her?
> +------------------------------------+
>
> becomes:
> +------------------------------------+
> 27
> 00:08:-06,898 --> 00:08:-03,316
> She's visiting--
>
> 28
> 00:08:-03,525 --> 00:08:00,277
> Who wants to know about her?
> +------------------------------------+
> The changes in this are:
> (01 becomes -06)
> (04 becomes -03)
> (07 becomes 00)
>
> Although not correct for srt syntax, these are manual fixable .
>
> Rgds,
> Jeri

Best regards,
Tony.
--
Science is facts; just as houses are made of stones, so is science made
of facts; but a pile of stones is not a house and a collection of facts
is not necessarily science.
-- Henri Poincaré

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