Forum Discussion

Lightofstorm's avatar
Lightofstorm
New Contributor
5 years ago
Solved

VBscript: Copy 2-dimensional array to another one

Hi,
i would like to work with 2-dimensional array in function. 
After I fill the array, I would like to choose, based on the condition, to witch array i will copy it.

Code in script MsgStatus:

Dim Array1 (50,1), Array2 (50,1) ' Global variables

Sub Main

 Dim Array3 (50,1)

  Filling Array3 with data 
 (Now i would like to copy Array3 to Array1)
MsgStatus.Array1 = Array3

  i found this solution here (http://www.vbforums.com/showthread.php?476931-Copying-of-one-Array-to-another), but its full VB, not only VBScript and its only for 1-dimensional.
End Sub

When i run this i obtain mismatch. Its VBscript so simple, that 2-dimensional copy of array need to be filled record one by one? :(
Thanks for help in advanced, i am accesible on Skype (nick Lightofstorm) 

  • Thanks for reply.

    Looks like I was probably thinking more about the situation than was really needed.

    I already have a solution:

2 Replies

  • BenoitB's avatar
    BenoitB
    Community Hero

    I know how to make that, at any dimension, in Javascript but you should do yourself the transcription in VBscript.

    I give here the code if that can help.

     

      /**
       *  Copie par valeur d'un objet
       *  @function
       *  @author Biache Benoit
       *  @memberof system
       *  @param {Object} O - Objet à copier
       *  @returns {Object} Renvoie un nouvel objet, copie réelle par valeur de la source
       */
      function copyArray(O) {
        var output, v, key;
        output = Array.isArray(O) ? [] : {};
        for (key in O) {
           v = O[key];
           output[key] = (typeof v === "object") ? copyArray(v) : v;
        }
        return output;
      }    
      

    So you use as simple as 

    let table1, table2;

    // Suppose table1 is filled with single data or array of data

    // ...

    // Copy

    table2 = copyArray(table1);

     

    Another solution, simplier and faster, is to go through Json ...

    • Lightofstorm's avatar
      Lightofstorm
      New Contributor

      Thanks for reply.

      Looks like I was probably thinking more about the situation than was really needed.

      I already have a solution: