Sign in to follow this  
Followers 0
-Ultima-

_ArrayDisplayTree()

8 posts in this topic

#1 ·  Posted (edited)

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

So yeah, I tend to use nested arrays a lot (I know, it's not an advisable practice, but it still has its uses). With the current _ArrayDisplay(), I couldn't view the nested arrays, and got pretty annoyed having to manually look through each nested array, so I ended up writing this function. It supports both nested arrays, and multi-dimensional arrays.

Theoretically, an array with an unlimited number of dimensions can be handled by this function, but AutoIt has a built-in hard limit (VAR_SUBSCRIPT_MAX), which limits the number of dimensions to 64. An unlimited number of nested arrays is possible, though. That said, I'm not really sure why people would need that many subarrays and dimensions, but it's still viewable with _ArrayDisplayTree() if you so desire ;)

_ArrayDisplayTree.au3

#Include "Array.au3"

; Example 1: Multi-dimensional array
Local $aMain[2][4] = [["Paul", "Jim", "Richard", "Louis"], [485.44, 160.68, 275.16, 320.00]]
_ArrayDisplayTree($aMain, "Example 1: Multi-dimensional array")

; Example 2: Nested multi-dimensional arrays
Local $aSub[2][2][2][1] = [[[["Paul"], ["Jim"]], [["Richard"], ["Louis"]]], [[[485.44], [160.68]], [[275.16], [320.00]]]]
$aMain[0][0] = $aSub
_ArrayDisplayTree($aMain, "Example 2: Nested multi-dimensional arrays")oÝ÷ Ù°+­¬»Ûjëh×6#Include-Once
#include <GUIConstants.au3>
#Include <GUITreeView.au3>

;===============================================================================
;
; Description:      Displays given array in a treeview, with multi-dimensional
;                   and nested arrays supported
; Parameter(s):     $avArray - Array to display
;                   $sTitle  - [optional] Title to use for window
;                   $iWidth  - [optional] Width to use for window
;                   $iHeight - [optional] Height to use for window
; Requirement(s):   None
; Return Value(s):  On Success -  Returns 1
;                   On Failure -  Returns 0 and sets @error to 1
; Author(s):        Ultima
; Note(s):          - This function can theoretically support arrays with an
;                     unlimited number of dimensions, though in reality, it is
;                     limited by VAR_SUBSCRIPT_MAX (an AutoIt hard limit)
;                   - Unlimited nested arrays are supported
;
;===============================================================================
Func _ArrayDisplayTree(Const ByRef $avArray, $sTitle = "Array: TreeView Display", $iWidth = 400, $iHeight = 300)
    If Not IsArray($avArray) Then Return SetError(1, 0, 0)

    Local $iOnEventMode = Opt("GUIOnEventMode", 0)
    Local $hGUI = GUICreate($sTitle, $iWidth, $iHeight, DEFAULT, DEFAULT, $WS_MINIMIZEBOX + $WS_MAXIMIZEBOX)
    Local $iTreeView = GUICtrlCreateTreeView(0, 0, $iWidth-5, $iHeight-30, $GUI_SS_DEFAULT_TREEVIEW + $TVS_NONEVENHEIGHT, $WS_EX_CLIENTEDGE)
    GUICtrlSetResizing($iTreeView, $GUI_DOCKBORDERS)

    __treeAdd($iTreeView, $avArray)
    GUISetState()

    While GUIGetMsg() <> $GUI_EVENT_CLOSE
    WEnd
    GUIDelete($hGUI)

    Opt("GUIOnEventMode", $iOnEventMode)

    Return 1
EndFunc ;==>_ArrayDisplayTree

; Private
Func __treeAdd(ByRef $iTreeView, Const ByRef $avArray, $iParent = 0, $sPath = "", $iDimension = 1)
    If UBound($avArray, 0) > $iDimension Then
        For $i = 0 To UBound($avArray, $iDimension)-1
            __treeAdd($iTreeView, $avArray, _GUICtrlTreeViewInsertItem($iTreeView, "[" & $i & "]", $iParent), $sPath & $i & "][", $iDimension+1)
        Next
    Else
        For $i = 0 To UBound($avArray, $iDimension)-1
            Local $vItem = Execute("$avArray[" & $sPath & $i & "]")
            If IsArray($vItem) Then
                __treeAdd($iTreeView, $vItem, _GUICtrlTreeViewInsertItem($iTreeView, "[" & $i & "]", $iParent))
            Else
                _GUICtrlTreeViewInsertItem($iTreeView, "[" & $i & "] " & $vItem, $iParent)
            EndIf
        Next
    EndIf
EndFunc ;==>__treeAdd

I would've preferred not to use Execute() if it could have been avoided :)

_ArrayDisplayTree.zip

Edited by -Ultima-

[ WinINet.au3 | Array.au3 (Optimized) | _UnixTimeParse() ]

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Very nice, really useful when it comes to multi-dim array

Edited by Generator

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Look carefully at the example... It's intentional. The point was to show that it can accept nested arrays (not just multi-dimensional arrays), and your post simply proves that it's working as expected/intended.

Edited by -Ultima-

[ WinINet.au3 | Array.au3 (Optimized) | _UnixTimeParse() ]

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Very nice! Your use of Execute is great, one place where it is needed. And the nested arrays are also nice.

Edited by RazerM

My Programs:AInstall - Create a standalone installer for your programUnit Converter - Converts Length, Area, Volume, Weight, Temperature and Pressure to different unitsBinary Clock - Hours, minutes and seconds have 10 columns each to display timeAutoIt Editor - Code Editor with Syntax Highlighting.Laserix Editor & Player - Create, Edit and Play Laserix LevelsLyric Syncer - Create and use Synchronised Lyrics.Connect 4 - 2 Player Connect 4 Game (Local or Online!, Formatted Chat!!)MD5, SHA-1, SHA-256, Tiger and Whirlpool Hash Finder - Dictionary and Brute Force FindCool Text Client - Create Rendered ImageMy UDF's:GUI Enhance - Enhance your GUIs visually.IDEA File Encryption - Encrypt and decrypt files easily! File Rename - Rename files easilyRC4 Text Encryption - Encrypt text using the RC4 AlgorithmPrime Number - Check if a number is primeString Remove - remove lots of strings at onceProgress Bar - made easySound UDF - Play, Pause, Resume, Seek and Stop.

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