Jump to content

Recommended Posts

Posted

Hi,

I've needed FileWriteFromArray to use with 2D-array.

Thats why I've made _FileWriteFromArray2D and co-function _FileReadToArray2D.

By set an delimiter for _FileReadToArray2D, an 2D-array will create. In this case all lines from given file must have the same count of delimiters. Otherwise an error occurs.

Array[0] respectively Array[0][0] include count of readed lines.

;==========================================================================================================================================
; Function:         _FileWriteFromArray2D($FILEPATH, $ARRAY [, $iROWstart=0 [, $iROWend=0 [, $iCOLstart=0 [, $iCOLend=0 [, $DELIM='|']]]]])
;
; Description:      Write 1D/2D array to file, 2D with delimiter between every entry
; 
; Parameter(s):     $FILEPATH   - path/filename of the file to be write
;                   $ARRAY      - array to write from
;      optional     $iROWstart  - start row-index, default 0
;      optional     $iROWend    - end row-index, default Ubound(array)-1
;      optional     $iCOLstart  - start column-index, default 0
;      optional     $iCOLend    - end column-index, default Ubound(array,2)-1
;      optional     $DELIM      - delimiter for 2D-array entries, default '|'
;
; Requirement(s):   None
;
; Return Value(s):  On Success - Returns -1
;                   On Failure - Returns 0 and sets @error = 1 (given array is'nt array); @error = 2 (unable to open filepath)
;
; Note:             If $iROWstart > $iROWend or $iCOLstart > $iCOLend the values will be swapped among
;
; Author(s):        BugFix ( bugfix@autoit.de )
;==========================================================================================================================================
Func _FileWriteFromArray2D($FILEPATH, $ARRAY, $iROWstart=0, $iROWend=0, $iCOLstart=0, $iCOLend=0, $DELIM='|')
    If Not IsArray($ARRAY) Then
        SetError(1)
        Return 0
    EndIf
    Local $Ubound = UBound($ARRAY)
    If $iROWend = 0 Then $iROWend = $Ubound-1
    Local $fh = FileOpen($FILEPATH, 2)
    If $fh = -1 Then
        SetError(2)
        Return 0
    EndIf
    Select
    Case $iROWstart < 0 Or $iROWstart > $Ubound-1
        $iROWstart = 0
        ContinueCase
    Case $iROWend < 0 Or $iROWend > $Ubound-1
        $iROWend = $Ubound-1
        ContinueCase
    Case $iROWstart > $iROWend
        $tmp = $iROWstart
        $iROWstart = $iROWend
        $iROWend = $tmp
    EndSelect
    Local $Ubound2nd = UBound($ARRAY, 2)
    If @error = 2 Then
        For $i = $iROWstart To $iROWend
            FileWriteLine($fh, $ARRAY[$i])
        Next
    Else
        If $iCOLend = 0 Then $iCOLend = $Ubound2nd-1
        Select
        Case $iCOLstart < 0 Or $iCOLstart > $Ubound2nd-1
            $iCOLstart = 0
            ContinueCase
        Case $iCOLend < 0 Or $iCOLend > $Ubound2nd-1
            $iCOLend = $Ubound2nd-1
            ContinueCase
        Case $iCOLstart > $iCOLend
            $tmp = $iCOLstart
            $iCOLstart = $iCOLend
            $iCOLend = $tmp
        EndSelect
        For $i = $iROWstart To $iROWend
            $tmp = ''
            For $k = $iCOLstart To $iCOLend
                If $k < $iCOLend Then
                    $tmp &= $ARRAY[$i][$k] & $DELIM
                Else
                    $tmp &= $ARRAY[$i][$k]
                EndIf
            Next
            FileWriteLine($fh, $tmp)
        Next
    EndIf
    FileClose($fh)
    Return -1
EndFunc ;==>_FileWriteFromArray2D

;==========================================================================================================================================
; Function:         _FileReadToArray2D($FILEPATH, $ARRAY [, $DELIM=-1])
;
; Description:      Read 1D/2D array from file, if $DELIM is given (<> -1) 2D array will created
; 
; Parameter(s):     $FILEPATH   - path/filename of the file to read in an array
;                   $ARRAY      - array variable to hold readed data
;      optional     $DELIM      - delimiter for 2D-array entries, default -1 (none 2D-array)
;
; Requirement(s):   None
;
; Return Value(s):  On Success - Returns -1
;                   On Failure - Returns 0 and sets @error = 1  (given file are not seperated with given delimiter or count of delimiters 
;                                are not equal); @error = 2 (unable to open filepath)
;
; Note:             If given file is delimited to create 2D-array ALL lines need the same count of delimiters, otherwise an error occurs!
;
; Author(s):        BugFix ( bugfix@autoit.de )
;==========================================================================================================================================
Func _FileReadToArray2D($FILEPATH, ByRef $ARRAY, $DELIM=-1)
    Local $fh = FileOpen($FILEPATH, 0), $line, $var, $n = 1
    If $fh = -1 Then
        SetError(2)
        Return 0
    EndIf
    If $DELIM <> -1 Then
        $line = FileReadLine($fh, 1)
        $var = StringSplit($line, $DELIM)
        If IsArray($var) Then
            $Ubound2nd = $var[0]
            Local $AR[1][$Ubound2nd]
            $AR[0][0] = 0
        Else
            SetError(1)
            Return 0
        EndIf
        While 1
            $line = FileReadLine($fh, $n)
            If @error = -1 Then ExitLoop
            $var = StringSplit($line, $DELIM)
            If IsArray($var) Then
                ReDim $AR[UBound($AR)+1][$Ubound2nd]
                For $i = 0 To $Ubound2nd-1
                    $AR[UBound($AR)-1][$i] = $var[$i+1]
                Next
                $AR[0][0] += 1
            Else
                SetError(1)
                Return 0
            EndIf
            $n += 1
        Wend
    Else
        Local $AR[1]
        $AR[0] = 0
        While 1
            $line = FileReadLine($fh, $n)
            If @error = -1 Then ExitLoop
            ReDim $AR[UBound($AR)+1]
            $AR[UBound($AR)-1] = $line
            $AR[0] += 1
            $n += 1
        WEnd
    EndIf
    FileClose($fh)
    $ARRAY = $AR
    Return -1
EndFunc ;==>_FileReadToArray2D

Best Regards BugFix  

  • Moderators
Posted

That's gotta be slow.... ?

Try storing all the information in a string buffer, then write it when the loops are done for a bit more speed.

Also, If I'm not mistaken, if you look in randallc's signature, you'll see these functions (probably faster if I know him) than the standards anyway.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

  • 2 years later...
  • 1 year later...
  • 4 years later...
  • Moderators
Posted

gcue,

This functionality was added to the standard FileWriteFrom/ReadToArray in v3,3,12,0. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

  • 1 year later...
Posted
On 26.9.2014 at 5:48 PM, Melba23 said:

This functionality was added to the standard FileWriteFrom/ReadToArray in v3,3,12,0

With _FileReadToArray2D from @BugFix you can specify start-,endrow and start-,endcolumn. I just modified his version: 

;==========================================================================================================================================
; Function:         _FileWriteFromArray2D($FILEPATH, $ARRAY [, $iROWstart=0 [, $iROWend=0 [, $iCOLstart=0 [, $iCOLend=0 [, $DELIM='|']]]]])
;
; Description:      Write 1D/2D array to file, 2D with delimiter between every entry
;
; Parameter(s):     $FILEPATH   - path/filename of the file to be write
;                   $ARRAY      - array to write from
;      optional     $iROWstart  - start row-index, default 0
;      optional     $iROWend    - end row-index, default Ubound(array)-1
;      optional     $iCOLstart  - start column-index, default 0
;      optional     $iCOLend    - end column-index, default Ubound(array,2)-1
;      optional     $DELIM      - delimiter for 2D-array entries, default '|'
;
; Requirement(s):   None
;
; Return Value(s):  On Success - Returns -1
;                   On Failure - Returns 0 and sets @error = 1 (given array is'nt array); @error = 2 (unable to open filepath)
;
; Note:             If $iROWstart > $iROWend or $iCOLstart > $iCOLend the values will be swapped among
;
; Author(s):        BugFix ( bugfix@autoit.de )
; modified:         autoBert
;==========================================================================================================================================
Func _FileWriteFromArray2D($FILEPATH, $ARRAY, $iROWstart = 0, $iROWend = 0, $iCOLstart = 0, $iCOLend = 0, $DELIM = '|')
    If Not IsArray($ARRAY) Then
        SetError(1)
        Return 0
    EndIf
    Local $Ubound = UBound($ARRAY)
    If $iROWend = 0 Then $iROWend = $Ubound - 1
    Local $fh = FileOpen($FILEPATH, 2)
    If $fh = -1 Then
        SetError(2)
        Return 0
    EndIf
    Select
        Case $iROWstart < 0 Or $iROWstart > $Ubound - 1
            $iROWstart = 0
            ContinueCase
        Case $iROWend < 0 Or $iROWend > $Ubound - 1
            $iROWend = $Ubound - 1
            ContinueCase
        Case $iROWstart > $iROWend
            $tmp = $iROWstart
            $iROWstart = $iROWend
            $iROWend = $tmp
    EndSelect
    Local $Ubound2nd = UBound($ARRAY, 2)
    If @error = 2 Then
        $tmp = ''
        For $i = $iROWstart To $iROWend
            $tmp &= $ARRAY[$i] & @CRLF
        Next
        FileWrite($fh, StringStripWS($tmp, 2))
    Else
        If $iCOLend = 0 Then $iCOLend = $Ubound2nd - 1
        Select
            Case $iCOLstart < 0 Or $iCOLstart > $Ubound2nd - 1
                $iCOLstart = 0
                ContinueCase
            Case $iCOLend < 0 Or $iCOLend > $Ubound2nd - 1
                $iCOLend = $Ubound2nd - 1
                ContinueCase
            Case $iCOLstart > $iCOLend
                $tmp = $iCOLstart
                $iCOLstart = $iCOLend
                $iCOLend = $tmp
        EndSelect
        $tmp = ''
        For $i = $iROWstart To $iROWend
            For $k = $iCOLstart To $iCOLend
                If $k < $iCOLend Then
                    $tmp &= $ARRAY[$i][$k] & $DELIM
                Else
                    $tmp &= $ARRAY[$i][$k] & @CRLF
                EndIf
            Next
        Next
        FileWrite($fh, StringStripWS($tmp, 2))
    EndIf
    FileClose($fh)
    Return -1
EndFunc   ;==>_FileWriteFromArray2D

this version builds whole string and write only one time to harddisk, so i hope it speeds up.

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...