简体   繁体   中英

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?

如果您不想使用正则表达式,请在命令模式下尝试以下操作:

for i in range(0, 89) | for j in range(0, 39) | put = 'Array['.i.']['.j.'] =' | endfor | endfor

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 <Ca> 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 <Cv>39j to block select the first index <Ca> 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM