VBScript Tutorials - Herong's Tutorial Examples - Version 5.20, by Dr. Herong Yang

Creating a Copy of an Array

This section provides a tutorial example on how to create a copy of an array quickly with the array assignment statement.

How do you create a copy of an array? Of course, you can first declare an empty new array. Then copy all elements from the original array into the new array with a "For Each" loop. But that's too slow.

If you read previous sections, you know that the quickest way to copy an array is to use the array assignment statement:

   scalar_variable = original_array_variable

The array assignment statement will automatically create a new array as a copy of the specified array for you. The receiving scalar variable will contain a reference to the new array, which will be a dynamic-size array regardless of the type of the original array.

To show you how to use an assignment to create a copy of an array, I wrote the following example, array_copy.html:

<html>
<body>
<!-- array_copy.html
 - Copyright (c) 2015, HerongYang.com, All Rights Reserved.
-->
<pre>
<script language="vbscript">
   aSite = Array("yahoo", "netscape", "microsoft")
   
   aTemp = aSite
   ReDim Preserve aTemp(3)
   aTemp(2) = aTemp(2) & ".com"
   aTemp(3) = "google.com"
   
   document.writeln("")
   document.writeln("The Original array:")
   document.writeln("   Lower bound: " & LBound(aSite))
   document.writeln("   Upper bound: " & UBound(aSite))
   document.writeln("   Elements:")
   For Each sSite In aSite
      document.writeln("      " & sSite )
   Next

   document.writeln("")
   document.writeln("The second copy of the array:")
   document.writeln("   Lower bound: " & LBound(aTemp))
   document.writeln("   Upper bound: " & UBound(aTemp))
   For Each sSite In aTemp
      document.writeln("      " & sSite )
   Next
</script>
</pre>
</body>
</html>

Here is the output:


The Original array:
   Lower bound: 0
   Upper bound: 2
   Elements:
      yahoo
      netscape
      microsoft

The second copy of the array:
   Lower bound: 0
   Upper bound: 3
      yahoo
      netscape
      microsoft.com
      google.com

The output confirms that the assignment statement "aTemp = aSite" created a true copy of the original array.

Table of Contents

 About This Book

 Introduction of VBScript - Visual Basic Scripting Edition

 Variant Data Type, Subtypes, and Literals

 Arithmetic Operations

 Numeric Comparison Operations and Logical Operations

 String Operations - Concatenation and Comparison

 Variable Declaration and Assignment Statement

 Expression and Order of Operation Precedence

 Statement Syntax and Statement Types

 Array Data Type and Related Statements

Array References and Array Assignment Statements

 Assigning an Array to a Scalar Variable

 Array References Work Like Arrays

 "Array()" Function - Returning a Scalar Reference of an Array

Creating a Copy of an Array

 "Type mismatch" Runtime Error - Assignments to Array Variables

 Conditional Statements - "If ... Then" and "Select Case"

 Loop Statements - "For", "While", and "Do"

 "Function" and "Sub" Procedures

 Built-in Functions

 Inspecting Variables Received in Procedures

 Error Handling Flag and the "Err" Object

 Regular Expression Pattern Match and Replacement

 scrrun.dll - Scripting Runtime DLL Library

 Creating Your Own Classes

 IE Web Browser Supporting VBScript

 IIS ASP Server Supporting VBScript

 WSH (Windows Script Host)

 References

 Printable Copy - PDF Version

Creating a Copy of an Array - Updated in 2015, by Dr. Herong Yang