p() : msgbox array and object and etc.

Post your working scripts, libraries and tools for AHK v1.1 and older
MrDoge
Posts: 161
Joined: 27 Apr 2020, 21:29

p() : msgbox array and object and etc.

27 Jul 2020, 13:52

I replace msgbox with this since it's shorter
any suggestions ?

Edit: you can now enter as many parameters as you want, they will be separated by {Space}

credits:
https://www.autohotkey.com/boards/viewtopic.php?t=64332#p275856 by jeeswg
https://autohotkey.com/board/topic/85201-array-deep-copy-treeview-viewer-and-more/ by GeekDude

Code: Select all

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#SingleInstance, force
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.

p(234)
p("fewf")
p([1,2,3])
p({1:"a", 2:"b", 3:"c"})
p({a:1,f:[1,3,4,{l:"34234"}], e:3, t:4})

foo:=2
bar:="hello"
arr:=[1,2,3]
obj:={a:1,b:4,c:9,d:16}
p(foo,bar,arr,obj) ;they are separated by {Space}

return

p(oArray="", params*)
{
    params.Insert(1, oArray)
    for k, v in params {
        
        if (k!=1)
            space:=" "
        
        if IsObject(v)
        {
            if IsArray(v)
                finalStr.=space "[" Array_Print(v) "]"
            else
                finalStr.=space "{" ObjectPrint(v) "}"
        }
        Else
            finalStr.=space v
    }
    msgbox % finalStr
    return
}

;https://www.autohotkey.com/boards/viewtopic.php?t=64332#p275856 by jeeswg
IsArray(oArray)
{
    local
    if !ObjCount(oArray)
        return 1
    if !(ObjCount(oArray) = ObjLength(oArray))
        || !(ObjMinIndex(oArray) = 1)
    return 0
    for vKey in oArray
        if !(vKey = A_Index)
        return 0
    return 1
}

;https://autohotkey.com/board/topic/85201-array-deep-copy-treeview-viewer-and-more/ by GeekDude
Array_Print(Array) 
{
    if Array_IsCircle(Array)
        return "Error: Circular reference"
    Output=
    For Key23, Value24 in Array
    {
        If (IsObject(Value24))
        {
            if IsArray(Value24)
                Output .= "[" . Array_Print(Value24) . "]"
            else
                Output .= "{" . ObjectPrint(Value24) . "}"
        }

        Else If Value24 is not number
            Output .= """" . Value24 . """"
        Else
            Output .= Value24

        Output .= ", "
    }
    StringTrimRight, OutPut, OutPut, 2
    Return OutPut
}

ObjectPrint(Array) 
{
    if Array_IsCircle(Array)
        return "Error: Circular reference"
    Output=
    For Key23, Value24 in Array
    {
        If Key23 is not Number
            Output .= """" . Key23 . """:"
        Else
            Output .= Key23 . ":"

        If (IsObject(Value24))
        {
            if IsArray(Value24)
                Output .= "[" . Array_Print(Value24) . "]"
            else
                Output .= "{" . ObjectPrint(Value24) . "}"
        }
        Else If Value24 is not number
            Output .= """" . Value24 . """"
        Else
            Output .= Value24

        Output .= ", "
    }
    StringTrimRight, OutPut, OutPut, 2
    Return OutPut
}

Array_IsCircle(Obj, Objs=0)
{
    if !Objs
        Objs := {}
    For Key23, Val in Obj
        if (IsObject(Val)&&(Objs[&Val]||Array_IsCircle(Val,(Objs,Objs[&Val]:=1))))
        return 1
    return 0
}

return

f3::Exitapp

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 47 guests