开发者

How to create multi-dimensional jagged arrays in VbScript?

开发者 https://www.devze.com 2022-12-31 00:49 出处:网络
I need to create multi-dimensional array of strings. Each row of the array can have varying number of strings. Something like the follwing code:

I need to create multi-dimensional array of strings. Each row of the array can have varying number of strings. Something like the follwing code:

twoDimension = Array(Array())  
ReDim Preserve twoDimension(3)  
For i = 0 to 2  
 If i = 1 Then  
  twoDimension(i开发者_StackOverflow中文版) = Array(1,2,3)  
 End If  
 If i = 2Then  
     twoDimension(i) = Array(1,2,3,4,5)  
   End If  
Next  


How about a dictionary

Set a = CreateObject("Scripting.Dictionary")
a.Add 0, Array(1,2,3)
a.Add 1, Array(4,5,6)
MsgBox a.Count
MsgBox a.Item(0)(2)
MsgBox a.Item(1)(1)


There's nothing wrong with having jagged arrays in VBScript. There are some minor issues with your code (ReDim to 3 but only assigning values to 2, unnecessarily using a For loop to assign values), but in general, that's the correct syntax to use.

Option Explicit

Dim twoDimension, i, j

twoDimension = Array(Array())  
ReDim Preserve twoDimension(2)

twoDimension(1) = Array(1,2,3)
twoDimension(2) = Array(1,2,3,4,5)

For i = 0 To UBound(twoDimension)
  For j = 0 To UBound(twoDimension(i))
    WScript.Echo "(" & i & "," & j & ") = " & twoDimension(i)(j)
  Next
Next
0

精彩评论

暂无评论...
验证码 换一张
取 消