- Offizieller Beitrag
Hi,
mich hat schon lange gestört, dass _FileWriteFromArray nur mit 1D-Arrays funktioniert.
Deshalb nun hier die Funktion für 1D und 2D -Arrays.
_FileWriteFromArray2D($FILEPATH, $ARRAY [, $iROWstart=0 [, $iROWend=0 [, $iCOLstart=0 [, $iCOLend=0 [, $DELIM='|']]]]])
$iROWstart - Index der Startzeile - Standard 0
$iROWend - Index der Endzeile - Standard (Ubound)-1
$iCOLstart - Index der Startspalte - Standard 0
$iCOLend - Index der Endspalte - Standard (Ubound,2)-1
$DELIM - Trennzeichen - Standard '|'
Bei 2D-Arrays werden pro Textzeile die Einträge der Spalten getrennt durch $DELIM geschrieben.
Spoiler anzeigen
;==========================================================================================================================================
; 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 ( [email='bugfix@autoit.de'][/email] )
;==========================================================================================================================================
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
EDIT:
Und hier auch gleich die 'Co-Funktion'
_FilereadToArray2D($FILEPATH, $ARRAY [, $DELIM=-1])
Das Array[0] bzw. Array[0][0] enthält die Anzahl der Elemente.
Durch Angabe des Trennzeichens wird ein 2D-Array erzeugt.
Dabei Beachten:
Alle Zeilen der Datei müssen die gleiche Anzahl von Trennzeichen enthalten, ansonsten wird die Funktion mit Fehler abgebrochen.
Spoiler anzeigen
;==========================================================================================================================================
; 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 ( [email='bugfix@autoit.de'][/email] )
;==========================================================================================================================================
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