PDA

View Full Version : WSH: Multidimensional dynamic arrays. Simple demonstration code included.



SparceMatrix
07-02-2006, 12:51 PM
I am stuck trying to create a multidimensional dynamic array in VBS. I cannot find a clue to figure out how to do it. How do you type an array to make one or more of the dimensions dynamic? Simply using "Dim MyArray() works for only one dimension, at least in the code below. Try the sample code below. The text file needs to be in the same directory you put your WSH file.

This is the ColArrayTEST.txt file:

Col 1 - Row 1; Col 2 - Row 1; Col 3 - Row 1
Col 1 - Row 2; Col 2 - Row 2; Col 3 - Row 2
Col 1 - Row 3; Col 2 - Row 3; Col 3 - Row 3
Col 1 - Row 4; Col 2 - Row 4; Col 3 - Row 4
Col 1 - Row 5; Col 2 - Row 5; Col 3 - Row 5
Col 1 - Row 6; Col 2 - Row 6; Col 3 - Row 6
Col 1 - Row 7; Col 2 - Row 7; Col 3 - Row 7
And this is the VBScript file:

' VB Script Document

Option Explicit

Dim x
Dim objFSO
Dim objTextFile, objOutPut, SplitOutPut

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutPut = objFSO.OpenTextFile("ColArrayTEST.txt")

Dim OutPut(7, 3)
'Dim OutPut() 'THIS DOES NOT WORK! HOW DO YOU MAKE THAT "x" DIMENSION BELOW DYNAMIC?

x= 0

Do Until objOutPut.AtEndOfStream
SplitOutPut = Split(objOutPut.ReadLine, ";")
OutPut(2, x) = SplitOutPut(2)
OutPut(0, x) = SplitOutPut(0)
OutPut(1, x) = SplitOutPut(1)
x = x + 1
Loop

Wscript.echo("Array Sample: " & OutPut(3, 0) & ", " & OutPut(3, 1) & ", " & OutPut(3, 2))
If you use "Dim OutPut()", you get an error, "Subscript out of range" for the line "OutPut(2, x) = SplitOutPut(2)". This is the place the compiler first encounters the second dimension.

I am completely stumped. Any and all tips or clues would be appreciated.

Ken Puls
07-02-2006, 09:42 PM
Have a look here (http://www.vbaexpress.com/forum/showthread.php?t=8581) for some information that may be helpful. :)

HTH,

SparceMatrix
07-04-2006, 12:28 PM
None of the cases at that post really address my problem as near as I can tell. The code there assumes that the size of either of the dimensions are always known. I need one dimension fixed to 3 and the other dynamic. We assume in advance in this code that "... Until objOutPut.AtEndOfStream" is not known in advance. We would like to apply a list of any length. As far as I can tell there is no property available for TextStream to find out how many lines of the list that "ReadLine" will encounter.

mvidas
07-05-2006, 07:35 AM
SM,
I've noted the changes I made, the ReDim Preserve line is really the key change though:Option Explicit

Dim x
Dim objFSO
Dim objTextFile, objOutPut, SplitOutPut

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutPut = objFSO.OpenTextFile("ColArrayTEST.txt")

Dim OutPut() '***** REMOVED THE FIXED DIMENSIONS

x= 0

Do Until objOutPut.AtEndOfStream
ReDim Preserve OutPut(2, x) '***** ADDED THIS LINE!
SplitOutPut = Split(objOutPut.ReadLine, ";")
OutPut(2, x) = SplitOutPut(2)
OutPut(0, x) = SplitOutPut(0)
OutPut(1, x) = SplitOutPut(1)
x = x + 1
Loop

objOutPut.Close '***** NEED TO CLOSE FILE!
Set objFSO = Nothing '***** SETTING VARIANT NON-ARRAY VARIABLES TO NOTHING
Set objOutPut = Nothing
Set objTextFile = Nothing 'YOU HAVENT EVEN USED THIS VARIABLE YET
set SplitOutPut = Nothing
'***** SWITCHED DIMENSIONS IN NEXT LINE FROM 3,0 TO 0,3, ETC
Wscript.echo("Array Sample: " & OutPut(0, 3) & ", " & OutPut(1, 3) & ", " & OutPut(2, 3))Matt