Jump to content

Process TXT like excel column


Terenz
 Share

Recommended Posts

I've updated the "add a column" feature to be able to actually add a column and not replace it:

; add column with values
$insert_column = 3 ; which column to enter
ReDim $aData[$aData[0][0]+1][ubound($aData, 2)+1]
For $i=1 To $aData[0][0]
    For $j=ubound($aData, 2)-1 to $insert_column Step -1
        $aData[$i][$j]=$aData[$i][$j-1]
    next
    $aData[$i][$insert_column-1]= 'Foobar'
Next
_ArrayDisplay($aData)   ; display the result

@Terenz, i really hope that by now you started coding it by yourself, rather than waiting for my reply. if you did, you probably already have it working, and you won't be needing this:

#include <Array.au3>
#include "_filearray2d.au3"

; declare variables
Dim $aData[1]
Global $sFile=@ScriptDir&'\data.txt'

_FileToArray2D($sFile,$aData,' - ') ; get initial data
_ArrayDisplay($aData)   ; display it for verification

; add index
For $i=1 To $aData[0][0]
    $aData[$i][0]&=$i
    $aData[$i][1]&=$i
Next
_ArrayDisplay($aData)   ; display the result

; add column with values
ReDim $aData[$aData[0][0]+1][3]
For $i=1 To $aData[0][0]
    $aData[$i][2]=$aData[$i][1]&' in 3rd column'
Next
_ArrayDisplay($aData)   ; display the result

; remove columns except 1st
ReDim $aData[$aData[0][0]+1][1]
_ArrayDisplay($aData)   ; display the result

this is based on the functions i mentioned earlier, but slightly modified. save this code by the name "_filearray2d.au3" in the same folder as the script above:

#include-Once

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

; #INDEX# =======================================================================================================================
; Title .........: FileArray2D
; AutoIt Version : 3.2.10++
; Language ......: English
; Description ...: Functions for creating en reading 2D arrays.
; Author(s) .....: Frans (SnArF) Ordelman
; ===============================================================================================================================

; #NO_DOC_FUNCTION# =============================================================================================================
; Not working/documented/implemented at this time
; ===============================================================================================================================

; #CURRENT# =====================================================================================================================
;_FileToArray2D
;_FileFromArray2D
; ===============================================================================================================================

; #INTERNAL_USE_ONLY# ===========================================================================================================
; ===============================================================================================================================

; #FUNCTION# ====================================================================================================================
; Name...........: _FileToArray2D
; Description ...: Creates a 2D array from a file.
; Syntax.........: _FileToArray2D($aFile, ByRef $aArray, $aDelim = ";", $aStartRow = 1, $aEndRow = 0)
; Parameters ....: $aFile - File to read
;                  $aArray  - 2D Array to create
;                  $aDelim - [Optional] String delimiter, default is ";"
;                  $aStartRow - [Optional] Row to start, default is 1
;                  $aEndRow = [Optional] Row to end, default is 0 (all)
; Return values .: Success - Index of last added item
;                  Failure - -1, sets @error
; Author ........: Frans (SnArF) Ordelman
; Modified.......: orbs (31/12/2013): added flag = 1 to StringSplit (2 calls)
; Remarks .......: $aEndRow positive stops importing at file-line nr. in file,
;                  a negative value will import all lines - last nr off lines
; Related .......: _FileFromArray2D
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _FileToArray2D($aFile, ByRef $aArray, $aDelim = ";", $aStartRow = 1, $aEndRow = 0)
    Local $aRead, $aRows, $aColums, $aString, $i, $k
    $aRead = FileOpen($aFile)
    If $aStartRow < 1 Then $aStartRow = 1
    $aRows = _FileCountLines($aFile) + 1 - $aStartRow
    If $aEndRow > 0 Then $aRows = $aRows - ($aRows - $aEndRow + $aStartRow - 1)
    If $aEndRow < 0 Then $aRows = $aRows + $aEndRow

    $aColums = UBound(StringSplit(FileReadLine($aRead, $aStartRow), $aDelim,1)) - 1

    FileSetPos($aRead, 0, $FILE_BEGIN)
    FileReadLine($aRead, -1 + $aStartRow)

    Local $aArrayTemp[$aRows + 1][$aColums]
    $aArrayTemp[0][0] = $aRows

    For $i = 1 To $aRows
        $aString = StringSplit(FileReadLine($aRead), $aDelim,1)
        For $k = 0 To $aColums - 1
            $aArrayTemp[$i][$k] = $aString[$k + 1]
        Next
    Next

    FileClose($aRead)
    $aArray = $aArrayTemp
EndFunc   ;==>_FileToArray2D

; #FUNCTION# ====================================================================================================================
; Name...........: _FileFromArray2D
; Description ...: Creates a file from a 2D array.
; Syntax.........: _FileFromArray2d($aFile, $aArray, $aDelim = ";", $aWriteMode = 2, $aStartRow = 0, $aEndRow = 0, $aStartCol = 0, $aEndCol = -1)
; Parameters ....: $aFile - File to create
;                  $aArray  - 2D Array to read
;                  $aDelim - [Optional] String delimiter, default is ";"
;                  $aWriteMode - [Optional] Mode to open the file in, default is 2
;                  $aStartRow - [Optional] Row to start, default is 0
;                  $aEndRow = [Optional] Row to end, default is 0 (all)
;                  $aStartCol = [Optional] Colum to start, default is 0
;                  $aEndCol = [Optional] Column to end, default is -1 (all)
; Return values .: Success - Index of last added item
;                  Failure - -1, sets @error
; Author ........: Frans (SnArF) Ordelman
; Modified.......:
; Remarks .......: $aEndRow positive stops importing at row nr. in array,
;                  a negative value will import all rows - last nr off rows
; Related .......: _FileToArray2D
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _FileFromArray2d($aFile, $aArray, $aDelim = ";", $aWriteMode = 2, $aStartRow = 0, $aEndRow = 0, $aStartCol = 0, $aEndCol = -1)
    Local $aWrite, $aUboundRow, $aUboundCol, $aLine, $aDelimNr, $i, $k

    If Not IsArray($aArray) Then
        SetError(1)
        Return 0
    EndIf

    $aWrite = FileOpen($aFile, $aWriteMode)
    If $aWrite = -1 Then
        SetError(2)
        Return 0
    EndIf

    $aUboundRow = UBound($aArray, 1) - 1
    $aUboundCol = UBound($aArray, 2) - 1
    $aDelimNr = StringLen($aDelim)

    If $aEndRow > $aUboundRow Or $aEndRow = 0 Then $aEndRow = $aUboundRow
    If $aEndRow < 0 Then $aEndRow = $aUboundRow + $aEndRow
    If $aEndCol > $aUboundCol Or $aEndCol = -1 Then $aEndCol = $aUboundCol
    If $aEndRow < $aStartRow Then
        SetError(3)
        Return 0
    EndIf

    For $i = $aStartRow To $aEndRow
        $aLine = ""
        For $k = $aStartCol To $aEndCol
            $aLine &= $aArray[$i][$k] & $aDelim
        Next
        $aLine = StringTrimRight($aLine, $aDelimNr)
        FileWriteLine($aWrite, $aLine)
    Next

    FileClose($aWrite)
EndFunc   ;==>_FileFromArray2d

save your data in the text file "data.txt" in the same folder as the script, then launch the script. enjoy!

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