Loading ...
Sorry, an error occurred while loading the content.

Re: calculations on result from search

Expand Messages
  • Gumnos (Tim Chase)
    ... 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
    • 0 Attachment
      > 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
    • Luis Marcelo de Mattos Zeri
      Thank you Roberto, Klaus, Mark and Trevor! All the solutions worked very well. Cheers, Marcelo
      Message 2 of 4 , May 3, 2004
      • 0 Attachment
        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.