Sign in to follow this  
Followers 0
chuckgcs

FileTo2dArray UDF modified for blank lines in source file

1 post in this topic

I really believe that this probably belongs in the Example Scripts Sub-Forum, but I can't put it there because I lack the volume of posts necessary.

This is a modified version of the UDF in the "Array2D.au3" include file. I was having a problem using the original version due to a couple of blank lines at the end of my delimited source file. The lines consisted of only CR/LFs and contained no delimiters. By adding the While-Wend loop to the UDF the problem is addressed.

The debug error message being generated was:

C:\Program Files\AutoIt3\Include\Array2D.au3 (555) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

$aArray[$r - 1][$c - 1] = $aStr[$c]

$aArray[$r - 1][$c - 1] = ^ ERROR

Hope this helps someone.

Func _FileTo2dArray($sFilePath, ByRef $aArray)
    ;--------------------------------------
    Local $hFile
    Local $str
    Local $r, $c
    Local $aLocal[1]
    Local $aStr
    If Not _FileReadToArray($sFilePath, $aLocal) Then
     SetError(1) 
     Return 0
    Endif
    $aStr = StringSplit($aLocal[1], ",")
    ; The next While-Wend loop prevents an error if the last line(s) of the input file are blank with no delimiters
      While Not StringInStr( $aLocal[$aLocal[0]] , ",")
         $aLocal[0] = $aLocal[0] -1
      Wend
    ReDim $aArray[$aLocal[0]][$aStr[0]]
    For $r = 1 To $aLocal[0]
        $aStr = StringSplit($aLocal[$r], ",")
        For $c = 1 To UBound($aArray, 2)
            $aArray[$r - 1][$c - 1] = $aStr[$c]
        Next
    Next
    ;_Array2dDisplay2($aArray, "Before Sort",0)
    Return
EndFunc   ;==>_FileTo2dArray

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0