## Re: calculations on result from search

Expand Messages
• ... this ... subtract ... How does this work for you? %s/ ( d d ):00/ =substitute( 0 .(substitute(submatch(1), ^0* , , g )-1), .* (.. ) , 1 , ). :00
Message 1 of 4 , May 2, 2004
> I have several files where I need to make some substitutions. In one of
this
> substitutions I need to make a calculation on a result from a search.
> For example, I have a column like this:
>
> 00:30
> 01:00
> 01:30
> 02:00
> ...
>
> I want to change only the lines that end with 00; on this lines I need to
subtract
> 1 from the number on left side of :. So, the result should be:

How does this work for you?

%s/\(\d\d\):00/\=substitute('0'.(substitute(submatch(1),
'^0*','','g')-1), '.*\(..\)', '\1', '').':00'

It's ugly, but it accomodates the wrinkle that vim considers leading zeros
an indicator of an octal number, which does funky stuff with the maths at
hand. So, it strips off the leading zero, decrements, tacks a leading zero
back onto it, and then pulls the two right-most characters. It doesn't
monkey with "00:00" any differently (you get a negative number--which may or
may not be the desired behavior)

Hope this helps,

-tim
• ... I ll try again! I m sure there s a better way (ie writing a function to catch inevitable errors) but this seems to work as you d expect ( except for those
Message 2 of 4 , May 2, 2004
Luis Marcelo de Mattos Zeri wrote:

>I want to change only the lines that end with 00; on this lines I need to subtract
>1 from the number on left side of :. So, the result should be:
>
>00:30
>00:00
>01:30
>01:00
>
>
I'll try again!

I'm sure there's a better way (ie writing a function to catch inevitable
errors) but this seems to work as you'd expect ( except for those errors

:%s@\(^\d\)\zs\(\d\)\ze:00@\=submatch(2)-1@gc

whats it mean?
@ is the separator ie :%s@this@that@
\( \) are for grouping
so

^ find the start of the line
\d followed by a digit

\zs now that you've found it, forget about it & start the actual
search here
:h \zs

\d find a digit (the one you want to change)
\ze:00 find :00 ie it must be present, but dont include it
\= evaluate the replacement bit as an expression
:h \=

submatch(2)-1 better just to point to :h submatch

Problem (if this relates to times):
it will turn a 0 into -1 so :00 becomes :0-1 NOT :59
which wouldn't be what you need.
Far better to write a script.

--
Mark
• Thank you Roberto, Klaus, Mark and Trevor! All the solutions worked very well. Cheers, Marcelo
Message 3 of 4 , May 3, 2004
Thank you Roberto, Klaus, Mark and Trevor!

All the solutions worked very well.

Cheers,

Marcelo

> On Sun, 2 May 2004, Luis Marcelo de Mattos Zeri wrote:
>
>> Hello,
>>
>> I have several files where I need to make some substitutions. In one
>> of this substitutions I need to make a calculation on a result from
>> a search. For example, I have a column like this:
>>
>> 00:30
>> 01:00
>> 01:30
>> 02:00
>> ...
>>
>> I want to change only the lines that end with 00; on this lines I need
>> to subtract 1 from the number on left side of :.
>
> :g/00\$/ exe "norm! 0\<C-X>"
>
> HTH
>
> Klaus
>
>
Your message has been successfully submitted and would be delivered to recipients shortly.