Advertisement
Alan72104

Autoit ConsoleWriteArray

Jul 28th, 2021 (edited)
658
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 3.68 KB | None | 0 0
  1. ; ConsoleWriteArray(ByRef $a, $nl = True, $nlOnNewEle = False, $indentForNewEle = " ", $out = True)
  2. ; Writes an array to the console
  3. ; Params
  4. ;   $a - The array to write
  5. ;   $nl - Whether to add a new line at the end
  6. ;   $nlOnNewEle - Whether to pretty print the array
  7. ;   $indentForNewEle - Indent for next element, indent stacks up if pretty print is enabled
  8. ;   $out - RESERVED FOR INTERNAL USE
  9. ; Returns
  10. ;   1. Nothing - If variable isn't an array
  11. ;   2. The original array passed in - If variable is an array
  12. ;      - Enables chaining for easy debug use
  13. ;        e.g. ConsoleWrite("Customer ID: " & $array[2][3])
  14. ;        =>   ConsoleWrite("Customer ID: " & ConsoleWriteArray($array)[2][3])
  15. Func ConsoleWriteArray(ByRef $a, $nl = True, $nlOnNewEle = False, $indentForNewEle = " ", $out = True)
  16.     If Not IsArray($a) Then
  17.         Return
  18.     EndIf
  19.     Local $dims = UBound($a, 0)
  20.     Local $s = ""
  21.     $s &= "{"
  22.     ConsoleWriteArray_internal($s, $a, 1, $dims, "", $nlOnNewEle, $indentForNewEle)
  23.     $s &= "}"
  24.     If $nl Then
  25.         $s &= @CRLF
  26.     EndIf
  27.     If $out Then
  28.         ConsoleWrite($s)
  29.         Return $a
  30.     Else
  31.         Return $s
  32.     EndIf
  33. EndFunc
  34.  
  35. Func ConsoleWriteArray_internal(ByRef $s, ByRef $a, $dim, $dims, $ref, $nlOnNewEle, $indentForNewEle)
  36.     Local $count = UBound($a, $dim)
  37.     If $dim = $dims Then
  38.         Local $ele
  39.         For $i = 0 To $count - 1
  40.             $ele = Execute("$a" & $ref & "[" & $i & "]")
  41.             Switch VarGetType($ele)
  42.                 Case "Double"
  43.                     $s &= $ele
  44.                     If Not IsFloat($ele) Then
  45.                         $s &= ".0"
  46.                     EndIf
  47.                 Case "String"
  48.                     $s &= '"' & $ele & '"'
  49.                 Case "Array"
  50.                     $s &= ConsoleWriteArray($ele, False, False, " ", False)
  51.                 Case "Map"
  52.                     $s &= "Map"
  53.                 Case "Object"
  54.                     $s &= ObjName($ele)
  55.                 Case "DLLStruct"
  56.                     $s &= "Struct"
  57.                 Case "Keyword"
  58.                     If IsKeyword($ele) = 2 Then
  59.                         $s &= "Null"
  60.                     Else
  61.                         $s &= $ele
  62.                     EndIf
  63.                 Case "Function"
  64.                     $s &= FuncName($ele) & "()"
  65.                 Case "UserFunction"
  66.                     $s &= FuncName($ele) & "()"
  67.                 Case Else
  68.                     $s &= $ele
  69.             EndSwitch
  70.             If $i < $count - 1 Then
  71.                 $s &= "," & $indentForNewEle
  72.             EndIf
  73.         Next
  74.     Else
  75.         Local $indent = $indentForNewEle
  76.         If $nlOnNewEle Then
  77.             $indent = ""
  78.             Local $indentBuf = $indentForNewEle
  79.             Local $repeatCount = $dim
  80.             While $repeatCount > 1
  81.                 If BitAND($repeatCount, 1) Then
  82.                     $indent &= $indentBuf
  83.                 EndIf
  84.                 $indentBuf &= $indentBuf
  85.                 $repeatCount = BitShift($repeatCount, 1)
  86.             WEnd
  87.             $indent &= $indentBuf
  88.         EndIf
  89.         For $i = 0 To $count - 1
  90.             If $nlOnNewEle Then
  91.                 $s &= @CRLF & $indent
  92.             EndIf
  93.             $s &= "["
  94.             ConsoleWriteArray_internal($s, $a, $dim + 1, $dims, $ref & "[" & $i & "]", $nlOnNewEle, $indentForNewEle)
  95.             If $nlOnNewEle And $dim + 1 < $dims Then
  96.                 $s &= @CRLF & $indent
  97.             EndIf
  98.             $s &= "]"
  99.             If $i < $count - 1 Then
  100.                 $s &= "," & $indent
  101.             EndIf
  102.         Next
  103.         If $nlOnNewEle And $dim = 1 Then
  104.             $s &= @CRLF
  105.         EndIf
  106.     EndIf
  107. EndFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement