Jump to content

_arraytodisplaystring For Displaying Arrays


neogia
 Share

Recommended Posts

I love Jdeb's _ArrayDisplay, but I often have a use for displaying multi-dimensional arrays, so I made this UDF, which handles up to 4-dimensional arrays, and even arrays nested within other arrays. So, without further ado, here it is:

03/17/06 - v1.0 Released

;===============================================================================
;
; Function Name:    _ArrayToDisplayString()
; Description:    Converts an Array of up to 4 dimensions into a string
;                       displaying the contents of the array.
;                       NOTE: now supports nested arrays
; Parameter(s):  $vDispVar - Array to convert
;                   $iStyle[optional] - 0 = Cascading style (Default)
;                   $iLevel - used for recursion, DO NOT CHANGE
; Return Value(s):  If array is passed - String representing array
;                   If non-array is passed - String representing variable
; Author(s):        Ben Brightwell
;
;===============================================================================
Func _ArrayToDisplayString($vDispVar, $iStyle = 0, $iLevel = 0)
    If $iStyle <> 0 And $iStyle <> 1 Then
        $iStyle = 0
    EndIf
    $iNumDims = UBound($vDispVar, 0); Number of dimensions of $vDispVar
    Local $sDisplayStr = ""
    Local $iDimCounter = ""; Main Dimension Counter
    Local $iCounter1 = ""; Nested Counter
    Local $iCounter2 = ""; Nested Counter
    Local $iCounter3 = ""; Nested Counter
    Local $iCounter4 = ""; Nested Counter
    Select
    Case $iNumDims == 1
        For $iCounter1 = 0 To UBound($vDispVar, 1) - 1
            If $iLevel > 0 Then
                For $i = 1 To $iLevel
                    $sDisplayStr &= "    "
                Next
            EndIf
            If IsArray($vDispVar[$iCounter1]) Then
                $sDisplayStr &= "[" & $iCounter1 & "]= " & @CRLF
            Else
                $sDisplayStr &= "[" & $iCounter1 & "]= "
            EndIf
            $sDisplayStr &= _ArrayToDisplayString($vDispVar[$iCounter1], $iStyle, $iLevel+1)
        Next
    Case $iNumDims == 2
        For $iCounter1 = 0 To UBound($vDispVar, 1) - 1
            If $iStyle == 0 Then
                For $i = 1 To $iLevel
                    $sDisplayStr &= "    "
                Next
                $sDisplayStr &= "[" & $iCounter1 & "]= " & @CRLF
            EndIf
            For $iCounter2 = 0 To UBound($vDispVar, 2) - 1
                If $iStyle == 0 Then
                    $sDisplayStr &= "    "
                EndIf
                If $iLevel > 0 Then
                    For $i = 1 To $iLevel
                        $sDisplayStr &= "    "
                    Next
                EndIf
                If $iStyle = 0 Then
                    If IsArray($vDispVar[$iCounter1][$iCounter2]) Then
                        $sDisplayStr &= "[" & $iCounter2 & "]= " & @CRLF
                    Else
                        $sDisplayStr &= "[" & $iCounter2 & "]= "
                    EndIf
                ElseIf $iStyle == 1 Then
                    If IsArray($vDispVar[$iCounter1][$iCounter2]) Then
                        $sDisplayStr &= "[" & $iCounter1 & "][" & $iCounter2 & "]= " & @CRLF
                    Else
                        $sDisplayStr &= "[" & $iCounter1 & "][" & $iCounter2 & "]= "
                    EndIf
                EndIf
                $sDisplayStr &= _ArrayToDisplayString($vDispVar[$iCounter1][$iCounter2], $iStyle, $iLevel+1)
            Next
        Next
    Case $iNumDims == 3
        For $iCounter1 = 0 To UBound($vDispVar, 1) - 1
            If $iStyle == 0 Then
                For $i = 1 To $iLevel
                    $sDisplayStr &= "    "
                Next
                $sDisplayStr &= "[" & $iCounter1 & "]= " & @CRLF
            EndIf
            For $iCounter2 = 0 To UBound($vDispVar, 2) - 1
                If $iStyle == 0 Then
                    For $i = 1 To $iLevel
                        $sDisplayStr &= "    "
                    Next
                    $sDisplayStr &= "    [" & $iCounter2 & "]= " & @CRLF
                EndIf
                For $iCounter3 = 0 To UBound($vDispVar, 3) - 1
                    If $iStyle == 0 Then
                        $sDisplayStr &= "         "
                    EndIf
                    If $iLevel > 0 Then
                        For $i = 1 To $iLevel
                            $sDisplayStr &= "    "
                        Next
                    EndIf
                    If $iStyle == 0 Then
                        If IsArray($vDispVar[$iCounter1][$iCounter2][$iCounter3]) Then
                            $sDisplayStr &= "[" & $iCounter3 & "]= " & @CRLF
                        Else
                            $sDisplayStr &= "[" & $iCounter3 & "]= "
                        EndIf
                    ElseIf $iStyle == 1 Then
                        If IsArray($vDispVar[$iCounter1][$iCounter2][$iCounter3]) Then
                            $sDisplayStr &= "[" & $iCounter1 & "][" & $iCounter2 & "][" & $iCounter3 & "]= " & @CRLF
                        Else
                            $sDisplayStr &= "[" & $iCounter1 & "][" & $iCounter2 & "][" & $iCounter3 & "]= "
                        EndIf
                    EndIf
                    $sDisplayStr &= _ArrayToDisplayString($vDispVar[$iCounter1][$iCounter2][$iCounter3], $iStyle, $iLevel+1)
                Next
            Next
        Next
    Case $iNumDims == 4
        For $iCounter1 = 0 To UBound($vDispVar, 1) - 1
            If $iStyle == 0 Then
                For $i = 1 To $iLevel
                    $sDisplayStr &= "    "
                Next
                $sDisplayStr &= "[" & $iCounter1 & "]= " & @CRLF
            EndIf
            For $iCounter2 = 0 To UBound($vDispVar, 2) - 1
                If $iStyle == 0 Then
                    For $i = 1 To $iLevel
                        $sDisplayStr &= "    "
                    Next
                    $sDisplayStr &= "    [" & $iCounter2 & "]= " & @CRLF
                EndIf
                For $iCounter3 = 0 To UBound($vDispVar, 3) - 1
                    If $iStyle == 0 Then
                        For $i = 1 To $iLevel
                            $sDisplayStr &= "    "
                        Next
                        $sDisplayStr &= "         [" & $iCounter3 & "]= " & @CRLF
                    EndIf
                    For $iCounter4 = 0 To UBound($vDispVar, 4) - 1
                        If $iStyle == 0 Then
                            $sDisplayStr &= "              "
                        EndIf
                        If $iLevel > 0 Then
                            For $i = 1 To $iLevel
                                $sDisplayStr &= "    "
                            Next
                        EndIf
                        If $iStyle == 0 Then
                            If IsArray($vDispVar[$iCounter1][$iCounter2][$iCounter3][$iCounter4]) Then
                                $sDisplayStr &= "[" & $iCounter4 & "]= " & @CRLF
                            Else
                                $sDisplayStr &= "[" & $iCounter4 & "]= "
                            EndIf
                        ElseIf $iStyle == 1 Then
                            If IsArray($vDispVar[$iCounter1][$iCounter2][$iCounter3][$iCounter4]) Then
                                $sDisplayStr &= "[" & $iCounter1 & "][" & $iCounter2 & "][" & $iCounter3 & "][" & $iCounter4 & "]= " & @CRLF
                            Else
                                $sDisplayStr &= "[" & $iCounter1 & "][" & $iCounter2 & "][" & $iCounter3 & "][" & $iCounter4 & "]= "
                            EndIf
                        EndIf
                        $sDisplayStr &= _ArrayToDisplayString($vDispVar[$iCounter1][$iCounter2][$iCounter3][$iCounter4], $iStyle, $iLevel+1)
                    Next
                Next
            Next
        Next
    EndSelect
    If $iNumDims == 0 Then
        $sDisplayStr &= $vDispVar & @CRLF
    EndIf
    Return $sDisplayStr
EndFunc

Here's a nice example script:

#include "ArrayToDisplayString.au3"

;Handles up to a 4-dimensional array of any size
;1-Dimensional Test
Dim $test1[2]
$test1[0] = "test1"
$test1[1] = "test2"
MsgBox(0,"", _ArrayToDisplayString($test1))
MsgBox(0,"", _ArrayToDisplayString($test1, 1))

;2-Dimensional Test
Dim $test2[2][2]
$test2[0][0] = "test1"
$test2[0][1] = "test2"
$test2[1][0] = "test3"
$test2[1][1] = "test4"
MsgBox(0,"", _ArrayToDisplayString($test2))
MsgBox(0,"", _ArrayToDisplayString($test2, 1))

;3-Dimensional Test
Dim $test3[2][2][2]
$test3[0][0][0] = "test1"
$test3[0][0][1] = "test2"
$test3[0][1][0] = "test3"
$test3[0][1][1] = "test4"
$test3[1][0][0] = "test5"
$test3[1][0][1] = "test6"
$test3[1][1][0] = "test7"
$test3[1][1][1] = "test8"
MsgBox(0,"", _ArrayToDisplayString($test3))
MsgBox(0,"", _ArrayToDisplayString($test3, 1))

;4-Dimensional Test
Dim $test4[2][2][2][2]
$test4[0][0][0][0] = "test1"
$test4[0][0][0][1] = "test2"
$test4[0][0][1][0] = "test3"
$test4[0][0][1][1] = "test4"
$test4[0][1][0][0] = "test5"
$test4[0][1][0][1] = "test6"
$test4[0][1][1][0] = "test7"
$test4[0][1][1][1] = "test8"
$test4[1][0][0][0] = "test9"
$test4[1][0][0][1] = "test10"
$test4[1][0][1][0] = "test11"
$test4[1][0][1][1] = "test12"
$test4[1][1][0][0] = "test13"
$test4[1][1][0][1] = "test14"
$test4[1][1][1][0] = "test15"
$test4[1][1][1][1] = "test16"
MsgBox(0,"", _ArrayToDisplayString($test4))
MsgBox(0,"", _ArrayToDisplayString($test4, 1))

;Also handles nested arrays!
;Nested Array Test
Dim $nested[2]
Dim $subtest1[2][2][2]
$subtest1[0][0][0] = "test1"
$subtest1[0][0][1] = "test2"
$subtest1[0][1][0] = "test3"
$subtest1[0][1][1] = "test4"
$subtest1[1][0][0] = "test5"
$subtest1[1][0][1] = "test6"
$subtest1[1][1][0] = "test7"
$subtest1[1][1][1] = "test8"
Dim $subtest2[2][2][2]
$subtest2[0][0][0] = "test9"
$subtest2[0][0][1] = "test10"
$subtest2[0][1][0] = "test11"
$subtest2[0][1][1] = "test12"
$subtest2[1][0][0] = "test13"
$subtest2[1][0][1] = "test14"
$subtest2[1][1][0] = "test15"
$subtest2[1][1][1] = "test16"
$nested[0] = $subtest1
$nested[1] = $subtest2
MsgBox(0,"", _ArrayToDisplayString($nested))
MsgBox(0,"", _ArrayToDisplayString($nested, 1))

Hope it's useful, enjoy!

ArrayToDisplayString.au3

TestADTS.au3

Edited by neogia

[u]My UDFs[/u]Coroutine Multithreading UDF LibraryStringRegExp GuideRandom EncryptorArrayToDisplayString"The Brain, expecting disaster, fails to find the obvious solution." -- neogia

Link to comment
Share on other sites

I love Jdeb's _ArrayDisplay, but I often have a use for displaying multi-dimensional arrays, so I made this UDF, which handles up to 4-dimensional arrays, and even arrays nested within other arrays.

Check out my _Array1Box() UDF:

http://www.autoitscript.com/forum/index.php?showtopic=12720

It will display 1 or 2 dimensional arrays inside of a GUI window. Also, if you click on an element before you close the box, it will return the index number of that element, so it's a good quick-and-dirty item selection routine, for debugging or whatever.

See also my _ArrayNMap() UDF:

http://www.autoitscript.com/forum/index.ph...indpost&p=90303

It is designed to show multi-dimensional (currently supports up to 6 dimensions, but can be easily modified to do more) and also supports multi-dimensional nested arrays (where an array exists inside the element of another array)

Link to comment
Share on other sites

Bah, I should've searched around more. The only other UDF I found was _ArrayDisplay2, and I didn't see what I needed. I wish I would've seen yours first! Would've saved me some time. Oh well...

[u]My UDFs[/u]Coroutine Multithreading UDF LibraryStringRegExp GuideRandom EncryptorArrayToDisplayString"The Brain, expecting disaster, fails to find the obvious solution." -- neogia

Link to comment
Share on other sites

  • 1 year later...

Hi neogia,

I like your UDF and have been using it but something is confusing me...

Using your example from another post:

#include <ArrayToDisplayString.au3>

Dim $test[2]
Dim $testSub1[2]
$testSub1[0] = "This"
$testSub1[1] = "Works"
Dim $testSub2[2]
$testSub2[0] = "So does"
$testSub2[1] = "This"
$test[0] = $testSub1
$test[1] = $testSub2

MsgBox(0,"Whole array", _ArrayToDisplayString($test))

;my addition below
MsgBox(1,"ELEMENT TEST",$test[1][1]) ; <-- this is what I'm confused about

why can't I get the last msgbox to work? I get the damn subscript dimention range exceeded error.

Link to comment
Share on other sites

multi dimensional = $array[1][2][3][4][nth].. a nested array is $array1[1] = $array2, what you have is a single array..

so its needs to be MsgBox(1,"ELEMENT TEST",$test[1]), thats why you get that error.

Hi neogia,

I like your UDF and have been using it but something is confusing me...

Using your example from another post:

#include <ArrayToDisplayString.au3>

Dim $test[2]
Dim $testSub1[2]
$testSub1[0] = "This"
$testSub1[1] = "Works"
Dim $testSub2[2]
$testSub2[0] = "So does"
$testSub2[1] = "This"
$test[0] = $testSub1
$test[1] = $testSub2

MsgBox(0,"Whole array", _ArrayToDisplayString($test))

;my addition below
MsgBox(1,"ELEMENT TEST",$test[1][1]) ; <-- this is what I'm confused about

why can't I get the last msgbox to work? I get the damn subscript dimention range exceeded error.

Don't bother, It's inside your monitor!------GUISetOnEvent should behave more like HotKeySet()
Link to comment
Share on other sites

  • 2 years later...

This will save me from writing this code..

for $i to ubound($array)-1
  consolewrite($array[$i] &@lf)
next

from scratch all the time!

here's a wrapper i wrote for it, displays results to the console

Dim $test[2]
Dim $testSub1[2]
$testSub1[0] = "This"
$testSub1[1] = "Works"
Dim $testSub2[2]
$testSub2[0] = "So does"
$testSub2[1] = "This"
$test[0] = $testSub1
$test[1] = $testSub2

_ArrayToDisplayConsole($test)

Func _ArrayToDisplayConsole($vDispVar, $iStyle = 0, $iLevel = 0)
    Local $sReturn = _ArrayToDisplayString($vDispVar, $iStyle, $iLevel)
    ConsoleWrite("Array("&UBound($vDispVar,0)&")" &@LF& StringReplace($sReturn, @CRLF, @lf) )
EndFunc
Edited by Kastout
Don't bother, It's inside your monitor!------GUISetOnEvent should behave more like HotKeySet()
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...