Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Redim of multidimesional array 1

Status
Not open for further replies.

JadeKnight

IS-IT--Management
Feb 23, 2004
94
0
0
NO
Hi, I've got a problem with a ReDim of a multidimesional array. I can't figur out what's wrong. Any help would be appreciated.

Error : Microsoft VBScript runtime error: Subscript out of range: '1'

Basically this is this line :

Code:
aIndex(iCnt,iDim1) = sLine

This is the whole sub wich is failing :

Code:
	'Open Virtual File System
	Set oVfs = oFso.OpenTextFile(s, FORREADING)

	iCnt = 0
	iDim0 = 0
	iDim1 = 1
	
		
	'Read Virtual File System into array
	Do Until oVfs.AtEndOfStream
		sLine = oVfs.ReadLine
		
		ReDim Preserve aIndex(iCnt,iCnt)

		aIndex(iCnt,iDim0) = Left(sLine, InStr(sLine, "|") -1)
		aIndex(iCnt,iDim1) = sLine
					
		iCnt = iCnt + 1
			
				
		
	Loop

I don't get it. If I declare the array with a fixed size wich I know is equal or greater than the result it works fine...
 
From the WSH documentation:

If you use the Preserve keyword, you can resize only the last array dimension, and you can't change the number of dimensions at all.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
If the indexing part of the line (Left(sLine, InStr(sLine, "|") -1)) is unique then use a dictionary. If it is not, then you could do one of a few things each with there own pros and cons:

- Add a count to the indexing portion and use a dictionary.
- Dim the array with a large enough value that it won't need a redim.
- Use ExecGlobal to dim the array after you know exactly how many lines there are.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Thx for pointing that out to me EBGreen. I wasen't aware of that limitation. I use multidimesional arrays seldom, Suddenly I discovered the need for dictionary, haven't really thought of it that way before :)...
 
And off course, it's worth a star... correct reply always is :)...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top