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.

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.