Jump to content

_PrintFromArray() - Print an array to the console.


guinness
 Share

Recommended Posts

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
Edited by guinness

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

can we replace $iIndex with $i - $iBase

Thanks for the example :)

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

You can, but I was aiming for the same output as _ArrayDisplay. Perhaps I should have geared this towards the same approach as _ArrayDisplay instead.

Edit: I've implemented, see above.

Edited by guinness

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Updated the original post by adding an additional example. Now _ArrayFill is called to create a random 2d array.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

your $iIndex idea is more efficient

but i was just asking instead of using a new variable you could try instead the same with ( $i - $iBase)

Likewise

#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, 1)
EndFunc   ;==>Example

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
Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

OK, I misread your post. Sorry. Done.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Here's one I wrote for Au3Int, but ended up not using because it is limited. I ended up using a different method that printed nested arrays and arrays of any dimensions.

Still, I kept this one around because the output is pretty. I was planning on adding more flags (like configuring borders) and also taking into account the console width.

#include <Array.au3>


Global Const $APF_ALLOWMULTILINE = 1
Global Const $APF_RIGHTALIGN = 6
Global Const $APF_RIGHTALIGNCOL = 2
Global Const $APF_RIGHTALIGNDATA = 4
Global Const $APF_PRINTROWNUM = 8

Global Const $APF_DEFAULT = $APF_PRINTROWNUM


Local $a[5][4] = [ _
        ["Foo", "Bar", "Hello", "World"], _
        ["Guinness", "Murphy's", "Beamish", "Marsten's"], _
        ["Bishop's Finger", "Spitfire (The bottle of Britain!)", "Fursty Ferret", "Hobgoblin"], _
        ["Bombardier", "London Pride", "Old Speckled Hen", "Sunbeam"], _
        ["San Miguel", "Stella Artois", "Grolsch", "Leffe"]]

_ArrayPrint2D($a, -1, -1, "", $APF_ALLOWMULTILINE, 20)


Func _ArrayPrint2D(ByRef Const $avArray, $iStart = -1, $iEnd = -1, $sHeaders = "", $iFlags = Default, $iMaxColWidth = -1)

    If Not IsArray($avArray) Then Return SetError(1, 0, 0) ; Not an array
    If UBound($avArray, 0) > 2 Then Return SetError(2, 0, 0) ; Greater than 2d

    If $iStart < 0 Then $iStart = 0
    If $iEnd < 0 Or $iEnd >= UBound($avArray) Then $iEnd = UBound($avArray) - 1

    If $iStart > $iEnd Then
        Local $iTmp = $iStart
        $iStart = $iEnd
        $iEnd = $iTmp
    EndIf

    If $iFlags = Default Then $iFlags = $APF_DEFAULT

    Local $fPrintRowNum = BitAND($iFlags, $APF_PRINTROWNUM) / $APF_PRINTROWNUM

    Local $aColumns[UBound($avArray, 2)][2] ; [Header, Width]
    Local $asHeaders = 0
    If $sHeaders <> "" Then $asHeaders = StringSplit($sHeaders, "|", 2)
    Local $sStr = ""

    ; Deal with row number column if requested
    Local $rowNumWidth
    Local $rowNumHeader

    If $fPrintRowNum Then
        If $sHeaders <> "" Then
            $rowNumHeader = $asHeaders[0]
            _ArrayDelete($asHeaders, 0)
        Else
            $rowNumHeader = "Row"
        EndIf

        $rowNumWidth = StringLen($rowNumHeader)

        If StringLen($iEnd) > $rowNumWidth Then $rowNumWidth = StringLen($iEnd)

        If $iMaxColWidth > 0 And $iMaxColWidth < $rowNumWidth Then
            $rowNumWidth = $iMaxColWidth
        EndIf

        If BitAND($iFlags, $APF_RIGHTALIGNCOL) Then
            $sStr &= StringFormat(" %" & $rowNumWidth & "s |", $rowNumHeader)
        Else
            $sStr &= StringFormat(" %-" & $rowNumWidth & "s |", $rowNumHeader)
        EndIf
    EndIf



    ; Get column informations
    For $x = 0 To UBound($avArray, 2) - 1
        If $x < UBound($asHeaders) Then
            $aColumns[$x][0] = $asHeaders[$x]
        Else
            $aColumns[$x][0] = "Column " & $x
        EndIf

        $aColumns[$x][1] = StringLen($aColumns[$x][0])

        For $y = $iStart To $iEnd
            If StringLen($avArray[$y][$x]) > $aColumns[$x][1] Then
                $aColumns[$x][1] = StringLen($avArray[$y][$x])
            EndIf
        Next

        If $iMaxColWidth > 0 And $iMaxColWidth < $aColumns[$x][1] Then
            $aColumns[$x][1] = $iMaxColWidth
        EndIf

        If BitAND($iFlags, $APF_RIGHTALIGNCOL) Then
            $sStr &= StringFormat(" %" & $aColumns[$x][1] & "." & $aColumns[$x][1] & "s |", $aColumns[$x][0])
        Else
            $sStr &= StringFormat(" %-" & $aColumns[$x][1] & "." & $aColumns[$x][1] & "s |", $aColumns[$x][0])
        EndIf
    Next
    $sStr = StringTrimRight($sStr, 1) & @CRLF


    ; Build the table string
    Local $aNextLines[1][UBound($avArray, 2)]
    $aNextLines[0][0] = 0
    Local $iExtra

    For $y = $iStart To $iEnd
        If $fPrintRowNum Then
            If BitAND($iFlags, $APF_RIGHTALIGNDATA) Then
                $sStr &= StringFormat(" %" & $rowNumWidth & "s |", $y)
            Else
                $sStr &= StringFormat(" %-" & $rowNumWidth & "s |", $y)
            EndIf
        EndIf

        For $x = 0 To UBound($avArray, 2) - 1
            If BitAND($iFlags, $APF_ALLOWMULTILINE) And StringLen($avArray[$y][$x]) > $aColumns[$x][1] Then
                $iExtra = Ceiling(StringLen(StringLen($avArray[$y][$x])) / $aColumns[$x][1])

                If $aNextLines[0][0] < $iExtra Then
                    ReDim $aNextLines[$iExtra + 1][UBound($avArray, 2)]
                    $aNextLines[0][0] = $iExtra
                EndIf

                For $i = 1 To $iExtra
                    $aNextLines[$i][$x] = StringMid($avArray[$y][$x], $i * $aColumns[$x][1] + 1, $aColumns[$x][1])
                Next
            EndIf

            If BitAND($iFlags, $APF_RIGHTALIGNDATA) Then
                $sStr &= StringFormat(" %" & $aColumns[$x][1] & "." & $aColumns[$x][1] & "s |", $avArray[$y][$x])
            Else
                $sStr &= StringFormat(" %-" & $aColumns[$x][1] & "." & $aColumns[$x][1] & "s |", $avArray[$y][$x])
            EndIf
        Next

        $sStr = StringTrimRight($sStr, 1) & @CRLF

        If $aNextLines[0][0] Then
            For $i = 1 To $aNextLines[0][0]
                If $fPrintRowNum Then $sStr &= StringFormat(" %" & $rowNumWidth & "s |", "")

                For $x = 0 To UBound($avArray, 2) - 1
                    If BitAND($iFlags, $APF_RIGHTALIGNDATA) Then
                        $sStr &= StringFormat(" %" & $aColumns[$x][1] & "." & $aColumns[$x][1] & "s |", $aNextLines[$i][$x])
                    Else
                        $sStr &= StringFormat(" %-" & $aColumns[$x][1] & "." & $aColumns[$x][1] & "s |", $aNextLines[$i][$x])
                    EndIf
                Next

                $sStr = StringTrimRight($sStr, 1) & @CRLF
            Next

            ReDim $aNextLines[1][UBound($avArray, 2)]
            $aNextLines[0][0] = 0
        EndIf
    Next

    Return ConsoleWrite($sStr)
EndFunc   ;==>_ArrayPrint2D
Link to comment
Share on other sites

Nice, I like the structure of the columns.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

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