Jump to content

_FileWriteFromArray - only write specified column(s)


Recommended Posts

hello world =)

i am trying to write a specific array column to a text file.  instead of looping through each row, i thought maybe someone has come up with a solution.. i was using this but found it to be pretty slow when dealing with larger data sets.

thank you in advance!

 

Link to comment
Share on other sites

Building your own array with wanted columns, rows and then use _FileWriteFromArray will be the one possible solution, or try this modified  _FileWriteFromArray2D

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

Link to comment
Share on other sites

just thought i would mention that _FileWriteFromArray() also writes to disk only once, so that's not the performance issue at hand.

if you are handling a very large array and very specific columns, then you should write your own function. something along these lines:

Global $aArray[1000][1000] ; we said very large arrays, remember?

Global $aRows = [0, 3, 6, 10, 11, 12, 13, 22, 913] ; wuold write to array only the specified rows in the specified order

Global $aColumns = [3, 8, 4, 15, 28, 14, 116, 44] ; wuold write to array only the specified columns in the specified order


Global $hFile = FileOpen('your file name here', 2)

For $iRow = 0 To UBound($aRows) - 1
    For $iColumn = 0 To UBound($aColumns) - 1
        FileWrite($hFile, $aArray[$aRows[$iRow]][$aColumns[$iColumn]] & @TAB)
    Next
    FileWrite($hFile, @CRLF)
Next

FileClose($hFile)

; error control, magic numbers, delimiter character, and range processing (allow something like "10-12" the row/column ranges instead of 10,11,12) are left as an exercise for the reader

 

if you need a consecutive range of columns (and/or a consecutive range of columns) that i offer my elaboration on _FileWriteFromArray(), which accepts additional two parameters (base and bound) for first column and last column:

Func _FileWriteFromArrayEx($sFilePath, Const ByRef $aArray, $iBase = Default, $iUBound = Default, $sDelimiter = "|", $iBaseCol = Default, $iUBoundCol = Default)
    Local $iReturn = 0
    ; Check if we have a valid array as an input.
    If Not IsArray($aArray) Then Return SetError(2, 0, $iReturn)

    ; Check the number of dimensions is no greater than a 2d array.
    Local $iDims = UBound($aArray, $UBOUND_DIMENSIONS)
    If $iDims > 2 Then Return SetError(4, 0, 0)

    ; Determine last entry of the array.
    Local $iLast = UBound($aArray) - 1
    If $iUBound = Default Or $iUBound > $iLast Then $iUBound = $iLast
    If $iBase < 0 Or $iBase = Default Then $iBase = 0
    If $iBase > $iUBound Then Return SetError(5, 0, $iReturn)
    If $sDelimiter = Default Then $sDelimiter = "|"

    ; >>>>>>>>>>
    Local $iLastCol
    If $iDims = 2 Then
        $iLastCol = UBound($aArray, $UBOUND_COLUMNS) - 1
        If $iUBoundCol = Default Or $iUBoundCol > $iLastCol Then $iUBoundCol = $iLastCol
        If $iBaseCol < 0 Or $iBaseCol = Default Then $iBaseCol = 0
        If $iBaseCol > $iUBoundCol Then Return SetError(6, 0, $iReturn)
    EndIf
    ; >>>>>>>>>>

    ; Open output file for overwrite by default, or use input file handle if passed.
    Local $hFileOpen = $sFilePath
    If IsString($sFilePath) Then
        $hFileOpen = FileOpen($sFilePath, $FO_OVERWRITE)
        If $hFileOpen = -1 Then Return SetError(1, 0, $iReturn)
    EndIf

    ; Write array data to file.
    Local $iError = 0
    $iReturn = 1 ; Set the return value to true.
    Switch $iDims
        Case 1
            For $i = $iBase To $iUBound
                If Not FileWrite($hFileOpen, $aArray[$i] & @CRLF) Then
                    $iError = 3
                    $iReturn = 0
                    ExitLoop
                EndIf
            Next
        Case 2
            Local $sTemp = ""
            For $i = $iBase To $iUBound
                $sTemp = $aArray[$i][$iBaseCol]
                ; >>>>>>>>>> For $j = 1 To UBound($aArray, $UBOUND_COLUMNS) - 1
                For $j = $iBaseCol + 1 To $iUBoundCol
                    $sTemp &= $sDelimiter & $aArray[$i][$j]
                Next
                If Not FileWrite($hFileOpen, $sTemp & @CRLF) Then
                    $iError = 3
                    $iReturn = 0
                    ExitLoop
                EndIf
            Next
    EndSwitch

    ; Close file only if specified by a string path.
    If IsString($sFilePath) Then FileClose($hFileOpen)

    ; Return the results.
    Return SetError($iError, 0, $iReturn)
EndFunc   ;==>_FileWriteFromArrayEx

 

Edited by orbs

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

  • Moderators

gcue,

No need for additional functions - just use _ArrayExtract to get the subarray you want to write to file:

#include <Array.au3>

; Here is the original array 
Global $aArray[10][10]
For $i = 0 To 9
    For $j = 0 To 9
        $aArray[$i][$j] = $i & " - " & $j
    Next
Next
_ArrayDisplay($aArray, "", Default, 8)

; Extract column 3
$aTempArray = _ArrayExtract($aArray, -1, -1, 3, 3)
_ArrayDisplay($aTempArray, "", Default, 8)

; Which you can now write to a file

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

 

Link to comment
Share on other sites

thanks for the suggestions... Melba, I've tried that approach but for some reason i am unable to extract the 0 column.. even in your example.. weird.

 

#include <Array.au3>

; Here is the original array
Global $aArray[10][10]
For $i = 0 To 9
    For $j = 0 To 9
        $aArray[$i][$j] = $i & " - " & $j
    Next
Next
_ArrayDisplay($aArray, "", Default, 8)

; Extract column 3
$aTempArray = _ArrayExtract($aArray, 0, 0, 0, 0)
_ArrayDisplay($aTempArray, "", Default, 8)

; Which you can now write to a file

 

Link to comment
Share on other sites

9 minutes ago, gcue said:

thanks for the suggestions... Melba, I've tried that approach but for some reason i am unable to extract the 0 column.. even in your example.. weird.

 

#include <Array.au3>

; Here is the original array
Global $aArray[10][10]
For $i = 0 To 9
    For $j = 0 To 9
        $aArray[$i][$j] = $i & " - " & $j
    Next
Next
_ArrayDisplay($aArray, "", Default, 8)

; Extract column 3
$aTempArray = _ArrayExtract($aArray, 0, 0, 0, 0)
_ArrayDisplay($aTempArray, "", Default, 8)

; Which you can now write to a file

 

#include <Array.au3>

; Here is the original array
Global $aArray[10][10]
For $i = 0 To 9
    For $j = 0 To 9
        $aArray[$i][$j] = $i & " - " & $j
    Next
Next
_ArrayDisplay($aArray, "", Default, 8)

; Extract column 3
$aTempArray = _ArrayExtract($aArray, -1, -1, 0, 0)
_ArrayDisplay($aTempArray, "", Default, 8)

; Which you can now write to a file

 

 

Regards,
 

Link to comment
Share on other sites

  • Moderators

gcue,

As Trong has shown above, you need to use -1 to get all rows/columns. That was a minor bug (mea culpa) which was fixed some time ago - what version of AutoIt are you running?

M23

Edit: We posted together - looks like my guess was correct.

Edited by Melba23

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

 

Link to comment
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
 Share

  • Recently Browsing   0 members

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