PDA

View Full Version : WSH: How do you dynamically set the length of one or more dimensions in an array?



SparceMatrix
07-01-2006, 03:37 PM
I think I understand that a dynamically set array is done like this:

Dim MyArray()
But, I have not been able to apply this in any prototypical example and I haven't seen any documentation either. Furthermore, I would like to apply a dynamic array with more than one dimension so that I can loop through some variables and create a array of columns, for example, of fixed number. And also, the "split" function is supposed to return a "variant" array and I need to apply this in this same context. Again, no documentation that I can find, and one might hope that one explanation might shed light on another.
This code is from http://cobalt1.cdc.net/caspdoc/html/vbscript_split_function.htm

Dim MyString, MyArray, Msg
MyString = "VBScriptXisXfun!"
MyArray = Split(MyString, "x", -1, 1)
' MyArray(0) contains "VBScript".
' MyArray(1) contains "is".
' MyArray(2) contains "fun!".
Msg = MyArray(0) & " " & MyArray(1)
Msg = Msg & " " & MyArray(2)
MsgBox Msg
What I would like to do is take this array and loop it through any number of times to just repeat the phrase into the array, for example. The array would be something like ...

VBScript is fun!
VBScript is fun!
VBScript is fun!
VBScript is fun!
VBScript is fun!
VBScript is fun!
VBScript is fun!
VBScript is fun!
...
... and the array would be of dimension MyArray(3, 8) in this case or MyArray(8, 3). But in either case the "8" would be replaced by a variable "x" somewhere in the code and I would hope that it looked something like MyArray(x, 3). But again, the array would be dynamic in that first dimension. I can manage already with a variable in array with a fixed dimension length, as in
Dim MyArray(3, 8)
Anybody have any idea what I am talking about? Any and all tips or clues would be appreciated.

ALe
07-04-2006, 08:56 AM
Redim?

mvidas
07-05-2006, 06:27 AM
I agree with ALe, you'll have to use redim (or redim preserve), and you can only use the highest-level dimension as the dynamic bound. For instance, you can redim preserve from a (2,0) to (2,1) but you cant go from (2,0) to (3,0). If you're wondering how to do this dynamically in your code, I'll add on to your sample code above:Dim MyString, MyArray, Msg, YourArray(), Cnt, i
MyString = "VBScriptXisXfun!"
MyArray = Split(MyString, "x", -1, 1)
' MyArray(0) contains "VBScript".
' MyArray(1) contains "is".
' MyArray(2) contains "fun!".
Cnt = 0
Do
ReDim Preserve YourArray(2, Cnt)
YourArray(0, Cnt) = MyArray(0)
YourArray(1, Cnt) = MyArray(1)
YourArray(2, Cnt) = MyArray(2)
Cnt = Cnt + 1
Loop Until MsgBox("YourArray has dimensions of (" & LBound(YourArray, 1) & _
" to " & UBound(YourArray, 1) & ", " & LBound(YourArray, 2) & " to " & _
UBound(YourArray, 2) & ")" & vbCrLf & "Would you like to increase it?", _
vbYesNo) = vbNo
Msg = YourArray(0, 0) & " " & YourArray(1, 0) & " " & YourArray(2, 0)
For i = 1 To Cnt - 1
Msg = Msg & vbCrLf & YourArray(0, i) & " " & YourArray(1, i) & " " & YourArray(2, i)
Next
MsgBox MsgMatt

jindon
07-25-2006, 01:56 AM
Dim MyString, MyArray
MyString = "VBScriptXisXfun!"
MyArray = Split(MyString, "x", -1, 1)
' MyArray(0) contains "VBScript".
' MyArray(1) contains "is".
' MyArray(2) contains "fun!".
MsgBox Join(MyArray)

Second part will be 2stories:
1)when you know x in advance
you can simply redim MyArray(x,3) or MyArray(3,x)

2) when you don't know x and need to expand with keeping elements in array
you can only expand last dimention
e.g
For x = 1 To 8
Redim Preserve YourArray(3,x)
For n = 1 To 3
YourArray(n,x) = Join(MyArray)
Next
Next
if you need to expand 1st dimention, use "Jagged Array", array in array, instead
e.g

Dim temp(1 To 3)
For x = 1 To 8
For n = 1 To 3
temp(n) = Join(MyArray)
Next
Redim Preserve YourArray(1 To x)
YourArray(x) = temp
Next
temp