Search code examples
arraysregexvimteststand

gVim and 2D array


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?

Solution

  • 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