Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2004

    Unanswered: read string into 2D array

    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

    I want to read into a two dimentional array.

    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?

  2. #2
    Join Date
    Mar 2004
    I would do something like this:
    ' dim variables
    dim strString, i, el
    dim arrArray(), arrTMP
    ' set init values
    i = 0
    strString = "12:00;10;12:15;20;12:30;30;12:45;40;13:00;50"
    ' split string into temporary 1-dimensional array 
    arrTMP = Split(strString,";")
    ' arrange all elements into 2-dimensional array
    for el = 0 to Ubound(arrTMP) step 2
    	ReDim Preserve arrArray(2,i)
    	arrArray(1,i) = arrTMP(el)
    	arrArray(2,i) = arrTMP(el+1)
    	i = i + 1		' increase index counter	
    ' display arrARRAY contents:
    Response.Write "<br>FINAL Array has " & i & " elements:"
    for x = 0 to i-1		' i-1 to undo the last increase in the For..Next loop
    	Response.Write "<br> Time: " & arrArray(1,x) & " - score: " & arrArray(2,x)

    ! REMARK: with this example, your string should NOT end with an ";" because this will generate a VBScript error on the line 'arrArray(2,i) = arrTMP(el+1)' (-> no element).
    If your string does end with a trailing ";" you need to add some code to prevent the error (.. maybe just cut the trailing ';' ).

    This is one way to go, there must be 100 other ways to do the same thing.

    Greetz, Pete.

  3. #3
    Join Date
    Mar 2004
    thanx torp..

    I've tried the code and I get the following line :

    ReDim Preserve arrArray(2,i) the following error

    Run-time error '9':

    Subscript out of range

    and I dont comme out what' s wrong

    thank you in advance for any suggetion or idea..

  4. #4
    Join Date
    Mar 2004
    Strange - I've ran the code on my server before posting it and it worked fine - and I don't really see where the problem could be ...

    What platform are you using?
    I am running this in a VBscript ASP page on an IIS 5.0.

    The problem seems to be the indexes (2, i) but since it's a ReDim of the array, I don't see how the i could be 'out of range'.

    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)

    Can you check the Help file of your platform for the Dim / ReDim function? Maybe there is a syntax difference ..


  5. #5
    Join Date
    Mar 2004


    thanx torp,

    you are right, it was a probleme of increasing the last index...

    thank you once again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts