简体   繁体   English

VBScript创建一个多维数组并添加到它?

[英]VBScript create a multi-dimensional array and add to it?

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. 这对我来说很糟糕哈哈,我几乎检查过谷歌搜索的每一页,我仍然不清楚如何去做。

I want to create a multi dimensional array in VB Script called data2. 我想在VB Script中创建一个名为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

Basically I'm trying to learn how to make a multi-dimensional array in VB script and add to it with a loop. 基本上我正在尝试学习如何在VB脚本中创建一个多维数组并使用循环添加到它。 What are some basic examples that can work in my case? 在我的案例中可以使用哪些基本示例?

(1) The best way to get an ADO resultset into a two-dimensional array is to use the .GetRows method. (1)将ADO结果集转换为二维数组的最佳方法是使用.GetRows方法。 Then your problem just vanishes. 然后你的问题就消失了。

(2) There are two kind of arrays in VBScript. (2)VBScript中有两种数组。 Fixed arrays are declared by specifying their UBounds: 通过指定UBounds来声明固定数组:

Dim aFix(2, 3)

They can't be resized. 它们无法调整大小。 Dynamic arrays can be changed by ReDim [Preserve] . 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()

if you want to start with an empty one. 如果你想从一个空的开始。 The catch 22 is: you can use Preserve only for the last dimension. catch 22是:您只能将Preserve用于最后一个维度。

(3) Your (3)你的

Dim data2()

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: 遗憾的是,'编译器'太愚蠢了,无法捕捉到VBScript无法正常处理的野兽:

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

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 a()语句的恶意被后来的ReDim将适当的动态数组存储到该变量中的事实所隐藏:

>> 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

Update wrt jmbpiano's comment: 更新wrt jmbpiano的评论:

(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. (1)我提供了证据证明你不能得到UBound变量变暗(),所以我坚持认为这些野兽是可憎的。 Just look at the question (or this one ) to see that using the () will give you trouble. 只要看看问题(或者这个问题),看看使用()会给你带来麻烦。

(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. (2)我说你应该使用ReDim a(KnownUbound) '声明'一个已知大小的动态数组,但是我没有给出这个成语'Option Explicit'兼容性的证据。 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)

output: 输出:

cscript 19888987.vbs
qed

This may be off-topic, but after seeing your exact code, why aren't you using the built-in ADO function: GetRows() ? 这可能是偏离主题的,但在看到您的确切代码后,为什么不使用内置的ADO函数: GetRows()

    sub grabdata
        SQL_query = "SELECT * FROM MSAccess_table"
        Set rsData = conn.Execute(SQL_query)
        If Not rsData.EOF Then aData = rsData.GetRows()         
    end sub

This returns all your column # as the first index, and the rows (data) in the second. 这将返回所有列#作为第一个索引,返回行(数据)作为第一个索引。

So to loop through it, you would: 所以要遍历它,你会:

If IsArray(aData) Then
    For x = lBound(aData,2) to uBound(aData,2) 'loops through the rows
        Col1 = aData(0,x)
        Col2 = aData(1,x)
        Col3 = aData(2,x)
        Response.Write "Row #" & x+1 & "<br>"
        Response.Write "This is the data in Column1: " & Col1 & "<br>"
        Response.Write "This is the data in Column2: " & Col2 & "<br>"
        Response.Write "This is the data in Column3: " & Col3 & "<br>"
    Next
End If

*NOTE: Rows (and columns) start on 0 in the array by default. *注意:默认情况下,行(和列)在数组中从0开始。

set rs = conn.execute(strQry)

arrRAY = rs.GetRows()

if isarray(arrRAY) then
  do stuff
end if

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM