Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


Photo

_arraytodisplaystring For Displaying Arrays


  • Please log in to reply
8 replies to this topic

#1 neogia

neogia

    Universalist

  • Active Members
  • PipPipPipPipPip
  • 269 posts

Posted 17 March 2006 - 11:11 PM

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

Plain Text         
;=============================================================================== ; ; 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:
Plain Text         
#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!

Attached Files


Edited by neogia, 17 March 2006 - 11:15 PM.

My UDFsCoroutine Multithreading UDF LibraryStringRegExp GuideRandom EncryptorArrayToDisplayString"The Brain, expecting disaster, fails to find the obvious solution." -- neogia







#2 blindwig

blindwig

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 772 posts

Posted 18 March 2006 - 12:17 AM

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)

#3 neogia

neogia

    Universalist

  • Active Members
  • PipPipPipPipPip
  • 269 posts

Posted 18 March 2006 - 12:35 PM

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...
My UDFsCoroutine Multithreading UDF LibraryStringRegExp GuideRandom EncryptorArrayToDisplayString"The Brain, expecting disaster, fails to find the obvious solution." -- neogia

#4 Teldin

Teldin

    Wayfarer

  • Active Members
  • Pip
  • 71 posts

Posted 12 April 2007 - 10:40 PM

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.

#5 Manadar

Manadar

         

  • MVPs
  • 10,866 posts

Posted 12 April 2007 - 10:42 PM

NICE!! :shocked: :(

Edited by Manadar, 12 April 2007 - 10:43 PM.


#6 Teldin

Teldin

    Wayfarer

  • Active Members
  • Pip
  • 71 posts

Posted 13 April 2007 - 12:15 AM

Perhaps I'm wrong but if you can't get $array[1][1] directly (like with msgbox) then wouldn't that mean the array is nested instead of multi-dimentional?

#7 WeMartiansAreFriendly

WeMartiansAreFriendly

    Where's the kaboom?

  • Active Members
  • PipPipPipPipPipPip
  • 1,245 posts

Posted 13 April 2007 - 12:35 AM

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.


Posted ImageDon't bother, It's inside your monitor!------GUISetOnEvent should behave more like HotKeySet()

#8 Teldin

Teldin

    Wayfarer

  • Active Members
  • Pip
  • 71 posts

Posted 13 April 2007 - 03:35 PM

Yes thanks, that does help clear things up. I am having problems making multidim arrays, I always come up with nested ones but now I can access my data! :shocked:

Thanks.

#9 WeMartiansAreFriendly

WeMartiansAreFriendly

    Where's the kaboom?

  • Active Members
  • PipPipPipPipPipPip
  • 1,245 posts

Posted 04 September 2009 - 01:20 AM

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, 04 September 2009 - 01:27 AM.

Posted ImageDon't bother, It's inside your monitor!------GUISetOnEvent should behave more like HotKeySet()




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users