Skip to content

Instantly share code, notes, and snippets.

@Chunjee
Forked from G33kDude/Array_Gui.ahk
Last active November 22, 2018 00:44
Show Gist options
  • Save Chunjee/a71f208bd55ebd7d3b0dc60847d07467 to your computer and use it in GitHub Desktop.
Save Chunjee/a71f208bd55ebd7d3b0dc60847d07467 to your computer and use it in GitHub Desktop.
; Function: Array_Gui
; Description: Displays an array as a treeview in a GUI
; Syntax: Array_Gui(Array)
; Parameters: Param1 - Array
; Return Value: Null
; Remarks: Resizeable
; Related: Array_Print, Array_DeepClone, Array_IsCircle
; Example: Array_Gui({"GeekDude":["Smart", "Charming", "Interesting"], "tidbit":"Weird"})
Array_Gui(Array, Parent="") {
if !Parent
{
Gui, +HwndDefault
Gui, New, +HwndGuiArray +LabelGuiArray +Resize
Gui, Margin, 5, 5
Gui, Add, TreeView, w300 h200
Item := TV_Add("Array", 0, "+Expand")
Array_Gui(Array, Item)
Gui, Show,, GuiArray
Gui, %Default%:Default
WinWait, ahk_id%GuiArray%
WinWaitClose, ahk_id%GuiArray%
return
}
For Key, Value in Array
{
Item := TV_Add(Key, Parent)
if (IsObject(Value))
Array_Gui(Value, Item)
else
TV_Add(Value, Item)
}
return
GuiArrayClose:
Gui, Destroy
return
GuiArraySize:
GuiControl, Move, SysTreeView321, % "w" A_GuiWidth - 10 " h" A_GuiHeight - 10
return
}
; Function:
; Array_Print
; Description:
; Quick and dirty text visualization of an array
; Syntax:
; Arrary_Print(Array)
; Parameters:
; Param1 - Array
; An array, associative array, or object.
; Return Value:
; A text visualization of the input array
; Remarks:
; Supports sub-arrays
; Related:
; Array_Gui, Array_DeepClone, Array_IsCircle
; Example:
; MsgBox, % Array_Print({"A":["Aardvark", "Antelope"], "B":"Bananas"})
Array_Print(Array) {
if Array_IsCircle(Array)
return "Error: Circular refrence"
For Key, Value in Array
{
If Key is not Number
Output .= """" . Key . """:"
Else
Output .= Key . ":"
If (IsObject(Value))
Output .= "[" . Array_Print(Value) . "]"
Else If Value is not number
Output .= """" . Value . """"
Else
Output .= Value
Output .= ", "
}
StringTrimRight, OutPut, OutPut, 2
Return OutPut
}
; Function:
; Array_DeepClone
; Description:
; Deep clone
; Syntax:
; Arrary_DeepClone(Array)
; Parameters:
; Param1 - Array
; An array, associative array, or object.
; Return Value:
; A copy of the array, that is not linked to the original
; Remarks:
; Supports sub-arrays, and circular refrences
; Related:
; Array_Gui, Array_Print, Array_IsCircle
; Example:
; Array1 := {"A":["Aardvark", "Antelope"], "B":"Bananas"}
; Array2 := Array_DeepClone(Array1)
Array_DeepClone(Array, Objs=0)
{
if !Objs
Objs := {}
Obj := Array.Clone()
Objs[&Array] := Obj ; Save this new array
For Key, Val in Obj
if (IsObject(Val)) ; If it is a subarray
Obj[Key] := Objs[&Val] ; If we already know of a refrence to this array
? Objs[&Val] ; Then point it to the new array
: Array_DeepClone(Val,Objs) ; Otherwise, clone this sub-array
return Obj
}
; Function:
; Array_IsCircle
; Description:
; Checks for circular refrences that could crash my other functions
; Syntax:
; Arrary_IsCircle(Array)
; Parameters:
; Param1 - Array
; An array, associative array, or object.
; Return Value:
; Boolean value according to whether it has a circular refrence
; Remarks:
; Takes an average of 0.023 seconds
; Related:
; Array_Gui, Array_Print(), Array_DeepClone()
; Example:
; Array1 := {"A":["Aardvark", "Antelope"], "B":"Bananas"}
; Array2 := Array_Copy(Array1)
Array_IsCircle(Obj, Objs=0)
{
if !Objs
Objs := {}
For Key, Val in Obj
if (IsObject(Val)&&(Objs[&Val]||Array_IsCircle(Val,(Objs,Objs[&Val]:=1))))
return 1
return 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment