ZetCode

VBScript 数组

最后修改于 2025 年 2 月 19 日

在本文中,我们将学习如何在 VBScript 中使用数组。数组用于将多个值存储在单个变量中。当您需要处理数据集集合时,它们非常有用。我们将使用 WScript.Echo 输出数组值,并使用 cscript 运行脚本。

简单数组示例

第一个示例演示了如何声明和使用简单数组。

simple_array.vbs
Dim fruits(2)
fruits(0) = "Apple"
fruits(1) = "Banana"
fruits(2) = "Cherry"

WScript.Echo "First fruit: " & fruits(0)

此示例声明了一个包含三个元素的数组 fruits 并为它们赋值。第一个元素使用 WScript.Echo 显示。

动态数组

VBScript 支持动态数组,其大小可以在运行时使用 ReDim 更改。

dynamic_array.vbs
Dim numbers()
ReDim numbers(2)

numbers(0) = 10
numbers(1) = 20
numbers(2) = 30

ReDim Preserve numbers(4)
numbers(3) = 40
numbers(4) = 50

WScript.Echo "Third number: " & numbers(2)

此示例显示了如何在保留现有值的同时调整数组大小。

多维数组

VBScript 支持多维数组,用于将数据存储在类似表格的结构中。

multi_dim_array.vbs
Dim matrix(2, 2)
matrix(0, 0) = 1
matrix(0, 1) = 2
matrix(1, 0) = 3
matrix(1, 1) = 4

WScript.Echo "Value at (1,1): " & matrix(1, 1)

此示例声明了一个二维数组并为其元素赋值。

数组长度

您可以使用 UBound 函数确定数组的长度。

array_length.vbs
Dim colors(2)
colors(0) = "Red"
colors(1) = "Green"
colors(2) = "Blue"

WScript.Echo "Array length: " & (UBound(colors) + 1)
Dim colors(2) 

我们声明了一个包含 3 个元素的数组(索引为 0 到 2)。

此示例计算并显示 colors 数组的长度。

遍历数组

您可以使用 For...Next 循环遍历数组。

iterate_array.vbs
Dim animals(2)
animals(0) = "Cat"
animals(1) = "Dog"
animals(2) = "Bird"

For i = 0 To UBound(animals)
    WScript.Echo "Animal: " & animals(i)
Next

此示例遍历 animals 数组并显示每个元素。

Run the script with cscript:
cscript iterate_array.vbs

Output:
Animal: Cat
Animal: Dog
Animal: Bird

对数组进行排序

VBScript 没有内置的数组排序函数,但您可以实现一个简单的排序算法。

sort_array.vbs
Dim nums(4)
nums(0) = 5
nums(1) = 3
nums(2) = 8
nums(3) = 1
nums(4) = 4

For i = 0 To UBound(nums) - 1
    For j = i + 1 To UBound(nums)
        If nums(i) > nums(j) Then
            temp = nums(i)
            nums(i) = nums(j)
            nums(j) = temp
        End If
    Next
Next

For i = 0 To UBound(nums)
    WScript.Echo "Number: " & nums(i)
Next

此示例将 nums 数组按升序排序。该示例使用冒泡排序算法,这种算法简单但不高效。

下一个示例使用更强大的快速排序算法。

sort_array2.vbs
Dim nums(4)
nums(0) = 5
nums(1) = 3
nums(2) = 8
nums(3) = 1
nums(4) = 4

' Quick Sort Function
Sub QuickSort(arr, low, high)
    Dim pivotIndex
    If low < high Then
        pivotIndex = Partition(arr, low, high)
        QuickSort arr, low, pivotIndex - 1
        QuickSort arr, pivotIndex + 1, high
    End If
End Sub

' Partition Function
Function Partition(arr, low, high)
    Dim pivot, i, j, temp
    pivot = arr(high)
    i = low - 1

    For j = low To high - 1
        If arr(j) <= pivot Then
            i = i + 1
            ' Swap arr(i) and arr(j)
            temp = arr(i)
            arr(i) = arr(j)
            arr(j) = temp
        End If
    Next

    ' Swap arr(i + 1) and arr(high)
    temp = arr(i + 1)
    arr(i + 1) = arr(high)
    arr(high) = temp

    Partition = i + 1
End Function

' Call QuickSort on the nums array
QuickSort nums, 0, UBound(nums)

' Print the sorted numbers
For i = 0 To UBound(nums)
    WScript.Echo "Number: " & nums(i)
Next

该程序使用快速排序算法对整数数组进行排序。

将数组传递给函数

您可以将数组传递给函数进行处理。

array_function.vbs
Function PrintArray(arr)
    For i = 0 To UBound(arr)
        WScript.Echo "Element: " & arr(i)
    Next
End Function

Dim values(2)
values(0) = 100
values(1) = 200
values(2) = 300

PrintArray(values)

此示例将 values 数组传递给 PrintArray 函数,该函数显示每个元素。

对象数组

VBScript 允许您创建对象数组。

object_array.vbs
Class Person
    Public Name
    Public Age
End Class

Dim people(1)
Set people(0) = New Person
people(0).Name = "Alice"
people(0).Age = 25

Set people(1) = New Person
people(1).Name = "Bob"
people(1).Age = 30

WScript.Echo "First person: " & people(0).Name

此示例创建了一个 Person 对象数组并访问它们的属性。

过滤数组

您可以使用循环和条件过滤数组。

filter_array.vbs
Dim numbers(4)
numbers(0) = 10
numbers(1) = 15
numbers(2) = 20
numbers(3) = 25
numbers(4) = 30

For i = 0 To UBound(numbers)
    If numbers(i) > 20 Then
        WScript.Echo "Filtered number: " & numbers(i)
    End If
Next

此示例过滤并显示大于 20 的数字。

合并数组

您可以使用循环将两个数组合并为一个。

combine_arrays.vbs
Dim arr1(2)
arr1(0) = "A"
arr1(1) = "B"
arr1(2) = "C"

Dim arr2(2)
arr2(0) = "D"
arr2(1) = "E"
arr2(2) = "F"

Dim combined(5)

For i = 0 To UBound(arr1)
    combined(i) = arr1(i)
Next

For i = 0 To UBound(arr2)
    combined(UBound(arr1) + 1 + i) = arr2(i)
Next

For i = 0 To UBound(combined)
    WScript.Echo "Combined: " & combined(i)
Next

此示例将 arr1arr2 合并到一个数组中。

在本文中,我们探讨了 VBScript 中处理数组的各种方面。我们涵盖了简单数组、动态数组、多维数组、数组长度、迭代、排序、将数组传递给函数、对象数组、过滤和合并数组。数组是 VBScript 中一项强大的功能,可让您高效地管理数据集。

作者

我叫 Jan Bodnar,是一名充满热情的程序员,拥有丰富的编程经验。我自 2007 年以来一直撰写编程文章。迄今为止,我已撰写了 1400 多篇文章和 8 本电子书。我在教授编程方面有十多年的经验。

列出所有 VBScript 教程