1

I have a for loop which iterates through an array generated from a CSV file. This creates an array with either 1, 2 or 3 sets of numbers. Problem is, I need to assign these a variable as I need to use them later on in a web form. Also; sometimes there may be only 1 set of numbers in the array (so only array(1) would be possible). How would I assign the dynamic array to individual variables - ie - YXVal, RXVal and GXVal for X and YYVal, RYVAL and GYVal for Y - but if there is only 1 in the array it would be YXVal and YYVal.

Do while NOT objTextFile.AtEndOfStream
    arrStr = split(objTextFile.ReadLine,",")
    for x = 1 to ubound(arrStr) 
        if arrStr(0) = "x" Then         
            Xval = arrStr(x)            
            response.write(XVal & "<br />")
        end if
    next    
    for y = 1 to ubound(arrStr)
        if arrStr(0) = "y" Then
            Yval = arrStr(y)            
            response.write(YVal & "<br />")
        end if
    next                
Loop
1
  • Are you expecting one occurrence of "x" and "y" in the file? Commented Jul 14, 2014 at 13:44

1 Answer 1

4

Here's a straightforward way. If you expect many occurrences of "x" and "y" and you need to process each occurrence, you should reinitialize your variable values at the start of each iteration. Otherwise, they may still contain values from the last iteration.

Do Until objTextFile.AtEndOfStream

    ' Reinitialize variable values. This may be optional, depending on your needs.
    YXVal = "" : RXVal = "" : GXVal = "" : YYVal = "" : RYVal = "" : GYVal = ""

    ' Read a line...
    arrStr = Split(objTextFile.ReadLine, ",")
    intSize = UBound(arrStr)

    If intSize >= 0 Then

        ' Parse line values...
        Select Case LCase(arrStr(0))
            Case "x"
                If intSize > 0 Then YXVal = arrStr(1)
                If intSize > 1 Then RXVal = arrStr(2)
                If intSize > 2 Then GXVal = arrStr(3)
            Case "y"
                If intSize > 0 Then YYVal = arrStr(1)
                If intSize > 1 Then RYVal = arrStr(2)
                If intSize > 2 Then GYVal = arrStr(3)
        End Select

    End If

Loop
Sign up to request clarification or add additional context in comments.

3 Comments

Many thanks - there is usually either 1 or 3 results, I suspect 2 wouldn't ever turn up, but using your code I keep getting an error on the line Select Case LCase(arrStr(0)) error code is Subscript out of range: '[number: 0]'
You probably have a blank line in your source file. I've added an If statement to test for it.
Spot on -- it's the simple things that confuse me :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.