Jump to content

Recommended Posts

Posted (edited)

G'day All

I've used _FileReadToArray in a lot of scripts and a few I needed to break up lines (IE a 2D array) so I thought I'd save time for next time and write a funciton to do it. BTW I did look for something like this but the ones I found didn't fit my needs.

Any constructive suggestions/improovments are appreaciated! Thanks!

Opt("MustDeclareVars", 1)
#AutoIt3Wrapper_Run_Debug_Mode=y


#include-once
#include <File.au3>
#include <Array.au3>

; .................. TEST purposes ONLY ................
Local $file

$file = FileOpen("C:\_FileReadTo2DArrayTest.txt", 2) ; which is similar to 2 + 8 (erase + create dir)
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
FileWriteLine($file, "1")
FileWriteLine($file, "1,2,3")
FileWriteLine($file, "1,2,3,4")
FileWriteLine($file, "5")
FileClose($file)

Local $atest, $iReturn
$iReturn = _FileReadTo2DArray("C:\_FileReadTo2DArrayTest.txt",$atest,",")
If $iReturn = 0 Then
    MsgBox(0,"ERROR", "@error = " & @error)
EndIf
_ArrayDisplay($atest)

$atest = ""
;TEST bad Delimiter
$iReturn = _FileReadTo2DArray("C:\_FileReadTo2DArrayTest.txt",$atest,";")
If $iReturn = 0 And @error <> 3 Then
    MsgBox(0,"ERROR : Bad Delimiter", "@error = " & @error)
EndIf
_ArrayDisplay($atest)

FileDelete("C:\_FileReadTo2DArrayTest.txt")

$atest = ""
;test missing file
$iReturn = _FileReadTo2DArray("C:\_FileReadTo2DArrayTest.txt",$atest,";")
If $iReturn = 0 And @error <> 1  Then
    MsgBox(0,"ERROR : Missing File", "@error = " & @error)
EndIf
_ArrayDisplay($atest)

; .................. TEST purposes ONLY ................




; #FUNCTION# ====================================================================================================================
; Name...........: _FileReadTo2DArray
; Description ...: Reads the specified file into a two dimensional array.
; Syntax.........: _FileReadTo2DArray($sFilePath, ByRef $aArray, $sDelimiter = ',')
; Parameters ....: $sFilePath  - Path and filename of the file to be read.
;                  $aArray     - The array to store the contents of the file.
;                  $sDelimiter - One or more characters to use as delimiters (case sensitive).
; Return values .: Success - Returns 1
;                       $array[0][0] = Number of rows/elements
;                       $array[0][1] = Number of Columns
;                  Failure - Returns 0
;                  @Error  - 0 = No error.
;                  |1 = Error opening specified file
;                  |2 = Unable to Split the file
;                  |3 = File doesn't contain multiple columns or Delimiter not found
; Author ........: John Morrison
; Remarks .......:
; Related .......: _FileWriteFrom2DArray
; Link ..........:
; Example .......:
; ===============================================================================================================================
Func _FileReadTo2DArray($sFilePath, ByRef $aArray, $sDelimiter = ',')
    Local $aTmpArray = ''
    If Not _FileReadToArray($sFilePath, $aTmpArray) Then
        Return SetError(@error, 0, 0)
    EndIf
    ;_ArrayDisplay($aTmpArray)

    If IsArray($aArray) Then
        ReDim $aArray[UBound($aTmpArray)][1]
    Else
        Dim $aArray[UBound($aTmpArray)][1]
    EndIf
    ;_ArrayDisplay($aArray)

    $aArray[0][0] = $aTmpArray[0] ; Set number of rows/elements
    Local $iCols = 1 ; Col count
    Local $aRowContent
    For $index = 1 To $aTmpArray[0]
        $aRowContent = StringSplit($aTmpArray[$index], $sDelimiter)

        If $aRowContent[0] > $iCols Then
            ;This line contains more columns than the array has been dimensioned for
            ; So redimensioned it - Yes I know this causes a lot of work but for most files
            ; this will only be done once or twice
            $iCols = $aRowContent[0]
            ReDim $aArray[UBound($aTmpArray)][$iCols]
        EndIf

        ;Copy line to actual array
        For $iColIndex = 1 To $aRowContent[0]
            $aArray[$index][$iColIndex-1] = $aRowContent[$iColIndex]
        Next
        ;_ArrayDisplay($aArray)
    Next
    If $iCols = 1 Then
        ; 3 = File doesn't contain multiple columns
        ; Single dimension array is returned
        Return SetError(3, 0, 0)
    EndIf
    $aArray[0][1] =  $iCols ; Final Column count
    Return 1 ;OK
EndFunc   ;==>_FileReadTo2DArray

Thanks all

John Morrison

aka

Storm-E

Edited by storme

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...