Improved MsgBox function for debugging.

Post your working scripts, libraries and tools for AHK v1.1 and older
Andrew1802
Posts: 23
Joined: 02 Jul 2020, 07:28

Improved MsgBox function for debugging.

17 Nov 2021, 21:15

Hello!
I find myself automating stuff by hooking into program memory lately and, inspired by maestrith's m() function, I decided to make my own version which would allow me to chain several variable and/or array prints within the same function, as well as change the formatting for each of them. It leverages a slightly modified version of st_printArr() from tidbit's String Things collection.

Code: Select all

st_printArr(array,depth:=10,indentLevel:="     ",format:="{:s}")
{	static parent,pArr,depthP=depth
	For k,v in (Array,(!IsObject(pArr)?pArr:=[]:""))
	{	Loop,% ((d2=depth&&dP2=depthP)?0:(depth-depthP+1,d2:=depth))
			parent:=SubStr(a:=SubStr(parent,1,InStr(parent,",",0,0)-1),1,InStr(a,",",0,0)),dP2:=depthP++
		k:=RegExReplace(k,","),list.=(indentLevel "arr[" pArr[depth]:=parent (k&1=""?"""" k """":k) "]"),((IsObject(v)&&depth>1)?(parent.=k ",",list.="`n" st_printArr(v,(depthP:=depth)-1,indentLevel "    ")):list.=" = " format(format,v)),list.="`n"
	}parent:=""
  return RTrim(list,"`n")
}

m(x*){
	formatNext:="{:s}"
	for i,v in x
		RegExMatch(v, "^<f>.*{.*}",formatStr)?(formatNext:=RegExReplace(formatStr, "^<f>")):(out.=(IsObject(v)?st_printArr(v,,,formatNext) "`n":(format(formatNext, v) "`n"),formatNext:="{:s}"))
	MsgBox, % out
}
Usage:

Code: Select all

m(variadic_list_of_references_or_formatting_strings*)
For every parameter you can pass either:
  • A variable / object reference
  • A simple value
OR
  • A formatting string which will be used to format the value of the NEXT parameter, preceded by an "<f>" tag
Example:

Code: Select all

a:=[1,2,3]
b:="abc"
c:=123
d:={a:1, b:2, c:3}
hex:="<f>0x{:x}"
dec:="<f>{:i}"
str:="<f>{:s}"
m(a,b,c,d,hex,a,b,hex,c,hex,d)
will print:
arr[1] = 1
arr[2] = 2
arr[3] = 3
abc
123
arr["a"] = 1
arr["b"] = 2
arr["c"] = 3
arr[1] = 0x1
arr[2] = 0x2
arr[3] = 0x3
abc
0x7b
arr["a"] = 0x1
arr["b"] = 0x2
arr["c"] = 0x3

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 119 guests