Search the Community
Showing results for tags 'printarray arrayconsole'.
-
_PrintFromArray() - Print an array to the console.
guinness posted a topic in AutoIt Example Scripts
Have you ever wanted to print an array to the console using ConsoleWrite instead of _ArrayDisplay? I needed a function like this yesterday for my _PathSplitEx thread, so I created this. It's similar to _FileWriteFromArray. Any suggestions for improvement(s) are very welcome. Function: ; #FUNCTION# ==================================================================================================================== ; Name ..........: _PrintFromArray ; Description ...: Print an array to the console. ; Syntax ........: _PrintFromArray(Const Byref $aArray[, $iBase = Default[, $iUBound = Default[, $sDelimeter = "|"]]]) ; Parameters ....: $aArray - [in/out and const] The array to be written to the file. ; $iBase - [optional] Start array index to read, normally set to 0 or 1. Default is 0. ; $iUBound - [optional] Set to the last record you want to write to the File. Default is whole array. ; $sDelimeter - [optional] Delimiter character(s) for 2-dimension arrays. Default is "|". ; Return values .: Success - 1 ; Failure - 0 and sets @error to non-zero ; |@error: ; |1 - Input is not an array. ; |2 - Array dimension is greater than 2. ; |3 - Start index is greater than the size of the array. ; Author ........: guinness ; Modified ......: ; Remarks .......: ; Related .......: _FileWriteFromArray ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _PrintFromArray(ByRef Const $aArray, $iBase = Default, $iUBound = Default, $sDelimeter = "|") ; Check if we have a valid array as input If Not IsArray($aArray) Then Return SetError(1, 0, 0) ; Check the number of dimensions Local $iDims = UBound($aArray, 0) If $iDims > 2 Then Return SetError(2, 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(3, 0, 0) If $sDelimeter = Default Then $sDelimeter = "|" ; Write array data to the console Switch $iDims Case 1 For $i = $iBase To $iUBound ConsoleWrite("[" & $i - $iBase & "] " & $aArray[$i] & @CRLF) Next Case 2 Local $sTemp = "" Local $iCols = UBound($aArray, 2) For $i = $iBase To $iUBound $sTemp = $aArray[$i][0] For $j = 1 To $iCols - 1 $sTemp &= $sDelimeter & $aArray[$i][$j] Next ConsoleWrite("[" & $i - $iBase & "] " & $sTemp & @CRLF) Next EndSwitch Return 1 EndFunc ;==>_PrintFromArray Example use of Function: #include <File.au3> Example() Func Example() Local $sDrive = '', $sDir = '', $sFileName = '', $sExtension = '' ConsoleWrite('_PrintFromArray Example 1:' & @CRLF) Local $aArray = _PathSplit('\\Server01\user\docs\Letter.txt', $sDrive, $sDir, $sFileName, $sExtension) _PrintFromArray($aArray) ConsoleWrite(@CRLF) $aArray = _ArrayFill(2) ; Fill a 2d array. _PrintFromArray($aArray) EndFunc ;==>Example ; #FUNCTION# ==================================================================================================================== ; Name ..........: _ArrayFill ; Description ...: Fill a dummy 1d or 2d array. ; Syntax ........: _ArrayFill($iType[, $fIsIndexCount = Default]) ; Parameters ....: $iType - 1 or 2 for the dimension of the array. ; $fIsIndexCount - [optional] Return the array count in the 0th index. Default is True. ; Return values .: Array ; Author ........: guinness ; Example .......: Yes ; =============================================================================================================================== Func _ArrayFill($iType, $fIsIndexCount = Default) Local $iRows = Random(2, 100, 1) If $fIsIndexCount = Default Then $fIsIndexCount = True EndIf Switch $iType Case 2 Local $iCols = Random(2, 10, 1) Local $aReturn[$iRows][$iCols] For $i = 0 To $iRows - 1 For $j = 0 To $iCols - 1 $aReturn[$i][$j] = 'Row ' & $i & ': Col ' & $j Next Next If $fIsIndexCount Then For $i = 0 To $iCols - 1 $aReturn[0][$i] = '' Next $aReturn[0][0] = $iRows - 1 EndIf Case Else Local $aReturn[$iRows] For $i = 0 To $iRows - 1 $aReturn[$i] = 'Row ' & $i Next If $fIsIndexCount Then $aReturn[0] = $iRows - 1 EndIf EndSwitch Return $aReturn EndFunc ;==>_ArrayFill