VBScript创建一个多维数组并添加到其中吗? [英] VBScript create a multi-dimensional array and add to it?

查看:161
本文介绍了VBScript创建一个多维数组并添加到其中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这对我来说是个难题,哈哈,我几乎检查了Google搜索的几乎每个页面,但我仍然不安静地了解如何操作.

This is a doozy for me haha, I've pretty much checked nearly every page on Google Search and I still don't quiet understand how to do it.

我想在VB脚本中创建一个名为data2的多维数组. 尝试看过的示例,但出现下标超出范围"错误

I want to create a multi dimensional array in VB Script called data2. Trying the examples that I've seen but I'm getting a "Subscript out of range" error

Dim data2()

sub grabdata
    SQL_query = "SELECT * FROM MSAccess_table"
    Set rsData = conn.Execute(SQL_query)
    Do Until rsData.EOF = True
        ReDim Preserve data2(UBound(data2) + 1)
        data2(UBound(data2)) = Array(rsData("id"),rsData("column_1"),rsData("column_2"),rsData("column_3"),rsData("column_4"))
    rsData.moveNext 
    Loop
end sub

基本上,我正在尝试学习如何在VB脚本中创建多维数组并通过循环将其添加到其中.在我的情况下可以使用的一些基本示例是什么?

Basically I'm trying to learn how to make a multi-dimensional array in VB script and add to it with a loop. What are some basic examples that can work in my case?

推荐答案

(1)将ADO结果集转换为二维数组的最佳方法是使用

(1) The best way to get an ADO resultset into a two-dimensional array is to use the .GetRows method. Then your problem just vanishes.

(2)VBScript中有两种数组. 固定数组是通过指定其UBounds来声明的:

(2) There are two kind of arrays in VBScript. Fixed arrays are declared by specifying their UBounds:

Dim aFix(2, 3)

无法调整大小. 动态数组可以通过ReDim [Preserve]进行更改.创建此类数组的最佳方法是

They can't be resized. Dynamic arrays can be changed by ReDim [Preserve]. The best way to create such an array is

ReDim aDyn(2, 3)

如果知道起始尺寸,或者

if you know the starting size, or

Dim aDyn : aDyn = Array()

如果您想从一个空的开始.陷阱22是:您只能将Preserve用于最后一个尺寸.

if you want to start with an empty one. The catch 22 is: you can use Preserve only for the last dimension.

(3)您的

Dim data2()

是可憎的-无大小的固定数组.遗憾的是,编译器"太愚蠢,无法捕捉到VBScript无法正确处理的野兽:

is an abomination - a fixed array of no size. It's a pity that the 'compiler' is too stupid to catch such a beast that VBScript can't handle properly:

>> Dim data2()
>> WScript.Echo UBound(data2)
>>
Error Number:       9
Error Description:  Subscript out of range

后面的ReDim会将适当的 dynamic 数组存储到该变量中的事实掩盖了Dim a()语句的缺点:

The nastiness of the Dim a() statement is hidden by the fact that a later ReDim will store a proper dynamic array into that variable:

>> Dim data2() ' <-- abomination
>> ReDim data2(1,1) ' <-- overwritten by a dynamic array
>> data2(0,0) = 0
>> ReDim Preserve data2(1,5) ' last dimension increased; 'old' data preserved
>> data2(1,5) = 1
>> WScript.Echo data2(0,0), data2(1,5)
>>
0 1

更新wrt jmbpiano的评论:

(1)我提供了证据,证明您无法使用()变暗的变量获得UBound,因此我坚持认为这种野兽是可憎的.只需查看问题(或)即可查看使用()会给您带来麻烦.

(1) I gave evidence that you can't get the UBound for a variable dimmed with (), so I stick to my claim that such beasts are abominations. Just look at the question (or this one) to see that using the () will give you trouble.

(2)我说过您应该使用ReDim a(KnownUbound)来声明"具有已知大小的动态数组,但是我没有为该惯用语的"Option Explicit"兼容性提供证据.所以:

(2) I said that you should use ReDim a(KnownUbound) to 'declare' a dynamic array with known size, but I didn't give evidence for the 'Option Explicit'-compatibility of this idiom. So :

Option Explicit
ReDim a(4711)
ReDim b(4,7,1,1)
a(0) = "qed"
b(0,0,0,0) = "qed"
WScript.Echo b(0,0,0,0)

输出:

cscript 19888987.vbs
qed

这篇关于VBScript创建一个多维数组并添加到其中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆