Click to See Complete Forum and Search --> : read string into twoD array
faamugol
03-26-2004, 05:15 AM
I want to read into a two dimentional arraythe following string:
One string contains 12:00;10;12:05;20;12:10;30;12:15;40......
So at 12:00 the score is 10
at 12:05 the score is 20 etc...
This string contains time and score separetaed by ";"
I would like to have the time in column 1 of the array and the score in the second column of the array. Like :
myAray[][0]=time values
myArray[][1]=score values
Can I use an integer array or should it be a char array?
Can anybody show the sample code for reading this text file into the 2D array?
I have tried split but it works only with one-dimentional array
tomhartland
03-26-2004, 08:35 AM
Not sure if you're trying to use JavaScript (using [] instead of () for your array indexes) but here is a VBScript version of what you're trying to achieve.
arArray1 = Split(strText,";",-1,1) ' Split text into parts
lNum = UBound(arArray1) + 1 ' Find the number of parts
If lNum Mod 2 = 0 Then ' Check there is an even number
ReDim arArray2(1,lNum/2) ' Create 2D array
For lLoop = 0 To lNum - 1
' Place into correct indexes of the 2nd array
arArray2(lLoop Mod 2,Int(lLoop/2)) = arArray1(lLoop)
Next
End If
If you had strText = "1;2;3;4" you would get...
arArray2(0,0) = "1"
arArray2(1,0) = "2"
arArray2(0,1) = "3"
arArray2(1,1) = "4"
Any help?
faamugol
03-26-2004, 08:57 AM
thank you for your answer,
with your idea I tried the following:
Dim strString
Dim dataArray()
Dim k, element
Dim arrTMP
strString = "12:00;0.001;12:15;0.000;12:30;0.0002;12:45;0.0003"
arrTMP = Split(strString, ";")
k= 0
For element = 1 To UBound(arrTMP) Step 2
ReDim Preserve dataArray(k, 2)
dataArray(k, 1) = arrTMP(el)
dataArray(k, 2) = arrTMP(el + 1)
k= k+ 1
Next
I get the following at line :
ReDim Preserve arrArray(2,i)
the following error
Run-time error '9': Subscript out of range
PS: I work in VB
I dont come out what' s wrong
thank you in advance for any suggetion or idea..
tomhartland
03-26-2004, 09:11 AM
When you are using multi-dimension arrays you can only ReDim the last dimension.
Swap your indexes over - make the first index the constant size, the second index the altered one.
Also, arrays in VBScript work with base 0.
If you declare an array as arArray(2) the size of the array is actually 3 (indexes 0, 1 and 2).
You should be aware of this, especially with multi-dimensions, as you could be wasting a lot of memory.
edit: You would also speed up the page by only doing one ReDim before the loop, as each ReDim with the Preserve has a lot of work to do. Better to create the full size array once and then work with that.
Tom :-)
faamugol
03-26-2004, 09:40 AM
the size of the array is not known in advance..so I have to redim in the loop
In any way the probleme is solved:
In VBScript you can ReDim any array including multi-dimentioned arrays - but only the last dimention can be increased.
So this would be valid: ReDim arrTMP(6,2,2,8,i)
This would NOT: ReDim arrTMP(i,2,2,8,10)
thank you once again for your time...
tomhartland
03-26-2004, 09:57 AM
You do know the size of the array before the loop, otherwise you would not be able to use the loop in the first place (hint: UBound).
You are correct about the ReDim - Only the last dimension can change.
(Your second statement would actually work - but only if 'i' was exactly the same value as when the array was originally created. If 'i' is different it will fail.)
Tom :-)