Link to home
Start Free TrialLog in
Avatar of Mike Caldwell
Mike CaldwellFlag for United States of America

asked on

Need help with simpl VBS array dimensioning

Instructions looke pretty clear, but not working for me.  I am populating an array to eventually write out to a file without knowing ahead of time how many records there will be.  I have tried this:

Dim newArray()
For n = 1 to 10
newArray(n) = n
Next

If I dimension with () I get an "Subscript out of range" error on line3.  If I dimenion without () I get a "Type mismatch" error.
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

You need to either do this:



Dim newArray(10), n

For n = 1 To 10
    newArray(n) = n
Next

Open in new window



Or:


Dim newArray(), n

ReDim newArray(10)
For n = 1 To 10
    newArray(n) = n
Next

Open in new window




In either case, be advised that the lower bound of the array will actually be 0, and not 1.
Avatar of Bill Prew
Bill Prew

A couple of additional thinks I'll mention.

First, it is possible to dynamically dimension an array at runtime, rather than statically in the original DIM statement. Since you mentioned not knowing the size you'll need, this might be useful.  Here's a quick little example of the REDIM statement in action.

' define array with no actual dimension
dim a()

' set variable to size of array we need
i = 10

' redimension the array to this size
redim a(i)

' add an entry to the array and display it
a(1) = 111
wscript.echo a(1)

Open in new window

Another powerful way to create an array at runtime based on an unknown number of elements is the SPLIT function. It chops apart a delimited list of values into array elements, and automatically dimensions the array size.  This can be very useful when trying to load a text file into an array for processing, as in:

Option Explicit
 
CONST ForWriting = 2
CONST ForReading = 1
 
Dim objFSO, strTextFile, strData, strLine, arrLines, n, objFile
 
'name of the text file
strTextFile = "EE27421002.txt"
 
'Create a File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
'Open the text file - strData now contains the whole file
Set objFile = objFSO.OpenTextFile(strTextFile,ForReading)
strData = objFile.ReadAll
objFile.close

'Split the text file into lines
arrLines = Split(strData,vbCrLf)
 
'Print each line
For Each strLine in arrLines
   Wscript.Echo strLine
Next

'Cleanup
Set objFile = Nothing
Set objFSO = Nothing

Open in new window

Hope this helps, questions welcome.

~bp
Avatar of Mike Caldwell

ASKER

Out of Range errors at the point of writing to the array.
Option Explicit
 
CONST ForWriting = 2
CONST ForReading = 1
CONST ForAppending = 8
 
Dim objFSO, strTextFile, strData, strLine, arrLines, n, objFile, nLines, strText, strTargetFile, LineCnt, strPos, strLen, strNew
Dim newArray(),m
 
'name of the text file
strTextFile = "D:\Junk\STRFILE.txt"
strTargetFile = "D:\Junk\STRFILE2.txt"
 
'Create a File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
'Open the text file - strData now contains the whole file
Set objFile = objFSO.OpenTextFile(strTextFile,ForReading)
strData = objFile.ReadAll
objFile.close

'Split the text file into lines
arrLines = Split(strData,vbCrLf)
 
nLines = UBound(arrLines)

m=0
For n = 0 to (nLines-1)

If Len(arrlines(n)) > 1 THEN
	strPos = Instr(arrLines(n),",")		' Find the comma
	strLen = Len(arrLines(n))
	strNew = Right(arrLines(n),strLen-strPos-1) & " " & Left(arrLines(n), (strPos - 1))
	newArray(m) = strNew & vbCrLf
	m = m + 1
ELSE
	m = m + 1
END IF
Next


Set objFile = objFSO.OpenTextFile(strTargetFile,ForAppending)

  for n = 0 to m			' ubound(arrLines)
    'strText = arrLines(n)     ' the vbCrLf will add a line break after each item
  objFile.WriteLine(newArray(n))
  next

objFile.close

msgbox "Done"

'Cleanup
Set objFile = Nothing
Set objFSO = Nothing

Open in new window

The range error occurs at line 34.
The variable m is used because the write file will have fewer lines than the read file.  Some line will be stripped out.  So m is a counter at to what array number is next for newArray, which is then written to the target file.
This gets an out of range error for n=1

Dim newArray(), n
For n = 1 To 100
wscript.echo n
    newArray(n) = n
Next
Here's what I have not made clear:  I will not know how many will be in the array until I am all done processing the input file.  So I need a dynamic dimension for newArray, but it seems that all methods suggested eventually require knowing how many there will be.
Is this what you had in mind?  It works, but seems like a lot of overhead to keep rediminsioning.
Dim newArray()

For n = 1 To 100
Redim preserve newArray(n)
wscript.echo n
    newArray(n) = n
Next

For m = 0 to 99
wscript.echo newArray(m)
next
SOLUTION
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks fellas.  My code has changed a lot due to change in end needs.  The key here was the fact of needed to redimension the target file with each write.  Sounds like  waste, but processed 10K lines faster than I can blink, so no problem.
Glad to have helped, thanks.

~bp