1

In my programming environment I have quite big 2D array. Its size is 90x40.

I have to fill this array by loading the data from external file.

The mechanism of loading the data consists of a binding file in which I have to do the binding in style like below:

Array[0][0] =
Array[0][1] =
Array[0][2] =
...
Array[20][37] = 
Array[20][38] =
...
Array[89][38] =
Array[89][39] =

It easy to compute that I have to create 3600 partially unique lines.

I thought that I can create the [..][..] elements in gVim and then add in front of them the name of the array. Although adding prefix is easy, I stuck on creating [..][..] bit.

In my scenario I want to solve this by doing something like:

  1. create 3600 rows
  2. add at the end of the each row/line (by using the :%s/$/\[ -- my expression 1 -- \]/g command) numbers from 0 to 89 in blocks of forty elements (forty zeros, forty ones, forty twos, etc.)
  3. add at the end of the each row/line (by using the :%s/$/\[ -- my expression 2-- \]/g command) numbers from 0 to 39 in blocks of forty elements (zero, one, two, ..., thirty nine, zero, one, ...,etc.)

my expression 1 would evaluate to the quotient of the operation (number of line) mod 90

my expression 2 would evaluate to the reminder of the operation (number of line) mod 40

And the questions now are:

  1. how to evaluate (number of line)
  2. how to calculate the (number of line) mod XX expression?
  3. maybe there is a better approach?
user1146081
  • 195
  • 15

2 Answers2

3

Try the following in command mode if you don't want to do it with regex:

for i in range(0, 89) | for j in range(0, 39) | put = 'Array['.i.']['.j.'] =' | endfor | endfor
Alex
  • 9,891
  • 11
  • 53
  • 87
2

VIM Macros are a better solution for this. You should write Array[0][0] on the first line, start recording a macro with qq, then yyp (copy and paste line), 2f[l to place the cursor on the second array index, press <C-a> to increment the number under cursor by 1 and then q to stop recording. Once you've done this just hit 38@q to repeat the macro 38 times to build the complete list.

Then you start recording a macro again with qq then 39k to go 39 lines up and V39jy39jp to select, copy and paste 39 lines then 0f[l to get to the first array index <C-v>39j to block select the first index <C-a> to increment it by one and q to end recording. Now hit 88@q to repeat the last step 88 times to get the desired output.

NOTE: The execution of the macro, especially the last one 88@q, will take sometime, so you'll have to be patient.

For the sake of completeness however, I'd like to mention how expression 1 be done. :%s/$/\=(line('.') - 1) % 90 and similarly for expression 2

Dhruva Sagar
  • 7,089
  • 1
  • 25
  • 34