Jump to content

Recommended Posts

Posted (edited)

Hello,

I just started to worte this function:

#include <Array.au3>

Local $aArray0[5] = ['xxx','ttttttt','zzzz','ttt']
Local $aArray[4][2] = [['adefxxxxxdo',''], _
['648fc','xxx'], _
['uiic','mmm'], _
[$aArray0,'']]

;~ _ArrayDisplay($aArray," (Line: "&@ScriptLineNumber&")")


;~ ConsoleWrite( &" (Line "&@ScriptLineNumber&")"&@CRLF)


ConsoleArrayWrite($aArray)


Func ConsoleArrayWrite($aArray,$DisplayFrom = 0)


    $yArray = UBound($aArray)-1
    $xArray = UBound($aArray,2)-1
    If $xArray < 0 Then
        ReDim $aArray[$yArray+1][1]
        $xArray = 0
    EndIf
    Local $xMaxStringLen[$xArray+1]
    Local $aTmp1[$yArray+1]
    Local $StringLens[$yArray+1][$xArray+1]
;~  Local $aWorkArray = $StringLens

    Local $xMaxStringLenStart = $xMaxStringLen


    Local $tmp1
    For $x = $DisplayFrom To $xArray
        For $y = $DisplayFrom To $yArray
;~          $aWorkArray[$y][$x] = $aArray[$y][$x]
            If IsArray($aArray[$y][$x]) Then $aArray[$y][$x] = '[Other Array]'
            $tmp1 = StringLen($aArray[$y][$x])
            $StringLens[$y][$x] = $tmp1
            $aTmp1[$y] = $tmp1
        Next
        ;_ArrayDisplay($aTmp1," (Line: "&@ScriptLineNumber&")")
        $xMaxStringLen[$x] = _ArrayMax($aTmp1, 1)+2
    Next


    Local $cOutput = '',$aRstring,$tmp2
    For $x = 0 To $xArray
        $tmp1 = ConsoleArrayWrite_Int1(Int(     ($xMaxStringLen[$x]-StringLen($x)+2)/2  ))
        $cOutput &= $tmp1&'['&$x&']'&$tmp1
    Next
    $cOutput &= @CRLF
    For $y = $DisplayFrom To $yArray
        $cOutput &= '['&$y&']'
        For $x = 0 To $xArray

            If $StringLens[$y][$x] = $xMaxStringLen[$x] Then
                $aRstring = $aArray[$y][$x]
            Else
                $tmp1 = Int(        ($xMaxStringLen[$x]-$StringLens[$y][$x])/2  )
                $tmp2 = ConsoleArrayWrite_Int1($tmp1)

                $aRstring = $tmp2&$aArray[$y][$x]&$tmp2
                $tmp2 = ($tmp1*2)+$StringLens[$y][$x]

                If $tmp2 < $xMaxStringLen[$x] Then $aRstring &= ConsoleArrayWrite_Int1($xMaxStringLen[$x]-$tmp2)


            EndIf

            $cOutput &= $aRstring
            If $x < $xArray Then $cOutput &= '|'
        Next
        If $y < $yArray Then $cOutput &= @CRLF
    Next
    ConsoleWrite($cOutput&@CRLF)
EndFunc


Func ConsoleArrayWrite_Int1($Count)
    Local $Output = ''
    For $a = 1 To $Count
        $Output &= ' '
    Next
    Return $Output
EndFunc
 
It works.
with the array is like in the example it print:
post-66583-0-10241000-1421360138_thumb.p
 
 
 
But if the array have more x elements. for example something like this:
Local $aArray0[5] = ['xxx','ttttttt','zzzz','ttt']
Local $aArray[4][10] = [['adefxxxxxdo',''], _
['648fc','xxx'], _
['uiic','mmm'], _
[$aArray0,'']]

then it print:

 post-66583-0-64750900-1421360275_thumb.p

 

And if one of the strings in the array have @CRLF or @LF then it print for example:

post-66583-0-99825700-1421360411_thumb.p

I do not know how to solve these problems.
It is very buggy prototype
but I this is a good start ..
Please help me solve these problems

Thanks

EDIT:

This:

Local $cOutput = '',$aRstring,$tmp2 = 0
    For $x = 0 To $xArray
        $tmp1 = ConsoleArrayWrite_Int1(Int(     ($xMaxStringLen[$x]-StringLen($x)+2-$tmp2)/2    ))
        $cOutput &= $tmp1&'['&$x&']'&$tmp1
        $tmp2 += 3
    Next

Instead of

Local $cOutput = '',$aRstring,$tmp2
    For $x = 0 To $xArray
        $tmp1 = ConsoleArrayWrite_Int1(Int(     ($xMaxStringLen[$x]-StringLen($x)+2)/2  ))
        $cOutput &= $tmp1&'['&$x&']'&$tmp1
    Next
 
Fix a bit the first problem...
With this fix the first problem start after 10:
post-66583-0-16310200-1421362504_thumb.p

 

Edited by Guest
Posted (edited)

I'm using this method to display an array to the console.

Might be helpful for you:

 

#include-once
#include <String.au3>

; #FUNCTION# ====================================================================================================================
; Name ..........: _ArrayDisplayConsole
; Description ...: Displays given 1D or 2D array in the console only
; Syntax ........: _ArrayDisplayConsole($avArray[, $iColumnWidth = 24[, $iItemLimit = -1[, $bDisplayCRNo = True[,
;                  $bDisplayValueBorders = False[, $sValueBorderChar = "*"[, $bShowBorder = False[, $sBorderChar = "|"[,
;                  $bCenter = False]]]]]]]])
; Parameters ....: $avArray              - Array to display
;                  $iColumnWidth         - [optional] String length of each cell
;                  $iItemLimit           - [optional] Maximum number of lines (rows) to show
;                  $bDisplayCRNo         - [optional] Display column / row numbers
;                  $bDisplayValueBorders - [optional] Display border around the array value to see any white space char
;                  $sValueBorderChar     - [optional] value border character
;                  $bShowBorder          - [optional] show the border of the cell
;                  $sBorderChar          - [optional] border of the cell
;                  $bCenter              - [optional] center the text in the columns
; Return values .: Success - 1
;                  Failure - 0, sets @error:
;                  |         1 - $avArray is not an array
;                  |         2 - $avArray has too many dimensions (only up to 2D supported)
; Author ........: UEZ
; Version .......: 0.92 build 2015-01-25 beta
; Modified ......:
; Remarks .......: If an array is found within the array it will be displayed as [ARRAY PTR]
; Related .......: UBound, IsArray, StringFormat, StringLeft, StringLen, ConsoleWrite
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _ArrayDisplayConsole($avArray, $iColumnWidth = 24, $iItemLimit = -1, $bDisplayCRNo = True, $bDisplayValueBorders = False, $sValueBorderChar = "*", $bShowBorder = False, $sBorderChar = "|", $bCenter = False)
    If Not IsArray($avArray) Then Return SetError(1, 0, 0)
    Local $iDimension = UBound($avArray, 0), $iUBound = UBound($avArray, 1) - 1, $iSubMax = UBound($avArray, 2) - 1
    If $iDimension > 2 Then Return SetError(2, 0, 0)
    If $iColumnWidth < 1 Or $iColumnWidth = Default Then $iColumnWidth = 24
    Local $iH, $iW, $iLenUBound = StringLen($iUBound), $sText, $iBorder = 1, $sArray = "[ARRAY PTR]"
    If $bShowBorder Then
        $iBorder = 0
    Else
        $sBorderChar = ""
    EndIf
    If $bDisplayCRNo And $iSubMax > -1 Then
        ConsoleWrite(StringFormat("%-" &  $iLenUBound + 2 & "s", " "))
        For $iW = 0 To $iSubMax
            $sText = "[" & StringLeft($iW, $iColumnWidth) & "]"
            Switch $bCenter
                Case True
                    ConsoleWrite(StringFormat("%-" & $iColumnWidth + $sBorderChar & "s",  _StringRepeat(" ", $iColumnWidth / 2 - 1) & $sText) & $sBorderChar )
                Case False
                    ConsoleWrite(StringFormat("%-" & $iColumnWidth + $sBorderChar & "s",  $sText) & $sBorderChar)
            EndSwitch
        Next
        ConsoleWrite(@CRLF)
    EndIf
    If $iItemLimit > 0 Then $iUBound = $iItemLimit - 1
    Switch $iDimension
        Case 1
            For $iH = 0 To $iUBound
                If $bDisplayCRNo Then ConsoleWrite(StringFormat("[%0" & $iLenUBound & "s] ", StringLeft($iH, $iColumnWidth)))
                If $bDisplayValueBorders Then
                    Switch IsArray($avArray[$iH])
                        Case False
                            $sText = $sValueBorderChar & StringLeft($avArray[$iH], $iColumnWidth) & $sValueBorderChar
                        Case True
                            $sText = StringLeft($sArray, $iColumnWidth) & $sValueBorderChar
                    EndSwitch
                    Switch $bCenter
                        Case True
                            ConsoleWrite(StringFormat("%-" & $iColumnWidth + $sBorderChar & "s", _StringRepeat(" ", ($iColumnWidth - StringLen($sText) + $sBorderChar) / 2) & $sText) & $sBorderChar)
                        Case False
                            ConsoleWrite(StringFormat("%-" & $iColumnWidth + $sBorderChar & "s", $sText) & $sBorderChar)
                    EndSwitch
                Else
                    Switch IsArray($avArray[$iH])
                        Case False
                            $sText = StringLeft($avArray[$iH], $iColumnWidth)
                        Case True
                            $sText = StringLeft($sArray, $iColumnWidth)
                        EndSwitch
                    Switch $bCenter
                        Case True
                            ConsoleWrite(StringFormat("%-" & $iColumnWidth + $sBorderChar & "s", _StringRepeat(" ", ($iColumnWidth - StringLen($sText) + $sBorderChar) / 2) & $sText) & $sBorderChar)
                        Case False
                            ConsoleWrite(StringFormat("%-" & $iColumnWidth + $sBorderChar & "s", $sText) & $sBorderChar)
                    EndSwitch
                EndIf
                ConsoleWrite(@CRLF)
            Next
        Case 2
            For $iH = 0 To $iUBound
                If $bDisplayCRNo Then ConsoleWrite(StringFormat("[%0" &  $iLenUBound & "s]", StringLeft($iH, $iColumnWidth)))
                For $iW = 0 To $iSubMax
                    If $bDisplayValueBorders Then
                        Switch IsArray($avArray[$iH][$iW])
                            Case False
                                $sText = $sValueBorderChar & StringLeft($avArray[$iH][$iW], $iColumnWidth) & $sValueBorderChar
                            Case True
                                $sText = $sValueBorderChar & StringLeft($sArray, $iColumnWidth) & $sValueBorderChar
                        EndSwitch
                        Switch $bCenter
                            Case True
                                ConsoleWrite(StringFormat("%-" & $iColumnWidth + $sBorderChar & "s", _StringRepeat(" ", ($iColumnWidth - StringLen($sText) + 1) / 2) & $sText) & $sBorderChar)
                            Case False
                                ConsoleWrite(StringFormat("%-" & $iColumnWidth + $sBorderChar & "s", $sText) & $sBorderChar)
                            EndSwitch
                    Else
                        Switch IsArray($avArray[$iH][$iW])
                            Case False
                                $sText = StringLeft($avArray[$iH][$iW], $iColumnWidth)
                            Case True
                                $sText = StringLeft($sArray, $iColumnWidth)
                        EndSwitch
                        Switch $bCenter
                            Case True
                                ConsoleWrite(StringFormat("%-" & $iColumnWidth + $sBorderChar & "s", _StringRepeat(" ", ($iColumnWidth - StringLen($sText) + 1) / 2) & $sText) & $sBorderChar)
                            Case False
                                ConsoleWrite(StringFormat("%-" & $iColumnWidth + $sBorderChar & "s", $sText) & $sBorderChar)
                        EndSwitch
                    EndIf
                Next
                ConsoleWrite(@CRLF)
            Next
    EndSwitch
    ConsoleWrite(@CRLF)
    Return 1
EndFunc

Example:

#include "_ArrayDisplayConsole.au3"

Local $avArray1[3][5] = [["Jon", "jpm", "Jos", "guinness", "GaryFrost"], _
                         ["Melba23", "SmOke_N", "big_daddy", "", ""], _
                         ["water", "YogiBear", "James", "BrewManNH", "Zedna"]]

_ArrayDisplayConsole($avArray1, 48, -1, True, False, "*", True, "|", True)

ConsoleWrite(@CRLF)

Local $avArray2[6] = ["DaleHolm", "trancexx", "TheSaint", "FireFox", "Xenobiologist", "Mat"]
_ArrayDisplayConsole($avArray2, 15)

Local $avArray3 = StringSplit(WinGetClassList("", ""), @LF)
_ArrayDisplayConsole($avArray3, 20)
 

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Posted (edited)

Thanks, UEZ :)

I will use it. But I like more my style..
 
 
Seems your function does not calculate alone the $iColumnWidth and does not centering each string unlike my function.
Maybe you can learn from my example the logic how to center and calculate $iColumnWidth (For each column) and upgrade your function based on this
Edited by Guest
Posted
  On 1/15/2015 at 11:20 PM, UEZ said:

Might be helpful for you:

very helpful :)

Thanks for sharing

mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

Updated the code from post#2 with following options:

  • center text in the cell
  • display border of the cell
Thanks to gil900 for the idea to center the value within a cell.

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...