Modify

Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#2514 closed Bug (Fixed)

_FileWriteFromArray - Count of elements in 2nd dimension is fixed to 3!

Reported by: BugFix Owned by:
Milestone: Component: AutoIt
Version: 3.3.8.1 Severity: None
Keywords: Cc:

Description

If you write an 2D-Array with more than 3 elements in columns, you get only 3 columns in the resulting file.
I've found the error:
The count of dimensions is detected with

Local $iDims = UBound($a_Array, 0)

Thats right, but than this value will also used as count of columns:

		Case 2
			Local $s_Temp
			For $x = $i_Base To $i_UBound
				$s_Temp = $a_Array[$x][0]
				For $y = 1 To $iDims

And so the file creating stops after 3rd column is reached.
Btw. In my mind it's slowly to write the file line by line.
Better way is collect the stuff and write it at once.
Here is my correct working and faster version of _FileWriteFromArray:

Func _FileWriteFromArray($File, $a_Array, $i_Base = 0, $i_UBound = 0, $s_Delim = "|")
	; Check if we have a valid array as input
	If Not IsArray($a_Array) Then Return SetError(2, 0, 0)
	Local $iDims = UBound($a_Array, 0)
	If $iDims > 2 Then Return SetError(4, 0, 0)

	; determine last entry
	Local $last = UBound($a_Array) - 1
	If $i_UBound < 1 Or $i_UBound > $last Then $i_UBound = $last
	If $i_Base < 0 Or $i_Base > $last Then $i_Base = 0

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

	; Write array data to file
	Local $s_Temp = '', $ErrorSav = 0
	Switch $iDims
		Case 1
			For $x = $i_Base To $i_UBound
				$s_Temp &= $a_Array[$x] & @CRLF
			Next
			If FileWrite($hFile, $s_Temp) = 0 Then $ErrorSav = 3
		Case 2
			For $x = $i_Base To $i_UBound
				$s_Temp &= $a_Array[$x][0]
;~ 				For $y = 1 To $iDims  ; == FAILURE -- we need "Ubound($a_Array, 2) -1" !!
				For $y = 1 To Ubound($a_Array, 2) -1
					$s_Temp &= $s_Delim & $a_Array[$x][$y]
				Next
				$s_Temp &= @CRLF
			Next
			If FileWrite($hFile, $s_Temp) = 0 Then $ErrorSav = 3
	EndSwitch

	; Close file only if specified by a string path
	If IsString($File) Then FileClose($hFile)

	; Return results
	If $ErrorSav Then Return SetError($ErrorSav, 0, 0)
	Return 1
EndFunc   ;==>_FileWriteFromArray

Attachments (0)

Change History (2)

comment:1 Changed 6 years ago by guinness

  • Resolution set to Fixed
  • Status changed from new to closed

comment:2 Changed 6 years ago by guinness

Already fixed in #2242.

Guidelines for posting comments:

  • You cannot re-open a ticket but you may still leave a comment if you have additional information to add.
  • In-depth discussions should take place on the forum.

For more information see the full version of the ticket guidelines here.

Add Comment

Modify Ticket

Action
as closed The ticket will remain with no owner.
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.