Sign in to follow this  
Followers 0
gil900

Need help with a function I am writing - ConsoleArrayWrite

5 posts in this topic

#1 ·  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 gil900

Share this post


Link to post
Share on other sites



#2 ·  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
1 person likes this

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

#3 ·  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 gil900

Share this post


Link to post
Share on other sites

Might be helpful for you:

very helpful :)

Thanks for sharing

mLipok


Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest begining - comunication with GitHub REST API *
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST API *

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * 

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2017-06-04

Share this post


Link to post
Share on other sites

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
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
Sign in to follow this  
Followers 0