2

My problem is to search all expressions in a file containing: (Scenario in converting from fortran to C)

Arr1 = Arr2[4:10]

and replace with

for(i = 0 ;i < 6; i++) Arr1[ i ] = Arr2[ i+3 ]

//comment i + 3 because i + "4-1". i< 6 because 6 = "10 - 4"

I tried this in vi but this is not working:

:%s/(\<\w+>)\s+=\s+(\<\w+>)[([0-9]):([0-9])]/\='for(i=0;i<'.(submatch(4) - submatch(3)).' ;i++) \1[i] = \2[i+'.(submatc h(3) - 1).']'/gc

Kindly help. The names of arrays used in file are not same. The array indices are not same for all arrays too. Hope the question is clear. I tried using \= and submatch() as in another answer but, that is not allowing separate strings containing arithmetic evaluation expressions to be concatenated. I am missing something.

2
  • The solution you mention works for me in gvim. What do you mean by not working? Does the substitution not work at all or is the code not compiling? In the latter case you might want to add a ; to the end of the line. Finally do you really intend to use vi or can you use vim?
    – Octaviour
    Commented Jan 19, 2017 at 14:37
  • I tried in vi and vim. The main problem am facing is \1 and \2 remained as is in replacement. In solution suggested by user9433424, \1 and \2 are not used but submatch() only in complete replacement string. I think mixing both either is not supported or there is something I need to know to mix both... Anyways, solution suggested user9433424 is working for me. Thank you.
    – DevBee
    Commented Jan 22, 2017 at 11:17

1 Answer 1

2

I'm not sure this is what you want, but you could try this substitution command:

%s/\v(<\w+>)\s+\=\s+(<\w+>)\[(\d+):(\d+)\]/\='for(i=0;i<'.(submatch(4) - submatch(3)).' ;i++) '.submatch(1).'[i] = '.submatch(2).'[i+'.(submatch(3) - 1).']'/gc
      |              |        |     |         |            |                                    |            |   |   |            |                          |
      |              |        |     |         |            |                                    |            |   |   |            |                          +-- flags
      |              |        |     |         |            |                                    |            |   |   |            +-- index of the second array item
      |              |        |     |         |            |                                    |            |   |   +-- name of the second array
      |              |        |     |         |            |                                    |            |   +-- assignment 1st array item = 2nd array item
      |              |        |     |         |            |                                    |            +-- index of the first array item
      |              |        |     |         |            |                                    +-- name of the first array
      |              |        |     |         |            +-- upper limit of the for loop
      |              |        |     |         +-- beginning of the replacement (for loop)
      |              |        |     +-- capture the number 10 inside `Arr2[4:10]`
      |              |        +-- capture the number 4 inside `Arr2[4:10]`
      |              +-- capture the name of the 2nd array
      +-- capture the name of the 1st array
2
  • it worked for me, Thank you, I realize \1 and \2 way doesnt work if there is at least one evaluated arithmetic expression in replacement string. I should use submatch() in all places in replacement string...Am I right?
    – DevBee
    Commented Jan 22, 2017 at 11:14
  • @Bhargav Yes, you're right. If you want to write a literal string in the replacement part of the substitution command, then you can use some metacharacters (&, \0, 1, ..., \9) to refer to (a part of) the text which was matched. But, if you want to write an expression which must be evaluated (the replacement part begins with \=), then you can't use these metacharacters anymore. But you can use submatch() instead. Commented Jan 22, 2017 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.