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:

<!-- array_copy.html
 - Copyright (c) 2015,, All Rights Reserved.
<script language="vbscript">
   aSite = Array("yahoo", "netscape", "microsoft")
   aTemp = aSite
   ReDim Preserve aTemp(3)
   aTemp(2) = aTemp(2) & ".com"
   aTemp(3) = ""
   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 )

   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 )

Here is the output:

The Original array:
   Lower bound: 0
   Upper bound: 2

The second copy of the array:
   Lower bound: 0
   Upper bound: 3

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)


 PDF Printing Version