Page 1 of 1

syntax for map

Posted: 18 Mar 2024, 10:59
by sanneedshelp
instead of this

Code: Select all

item := Map('value1', '(answer1)','value2', '(answer2)')
msgbox item

i want to do something like this . How can i achieve ?

Code: Select all

mapvar := ('value1', '(answer1)','value2', '(answer2)')
	
item := Map(mapvar)

msgbox item

[Mod edit: Added [code][/code] tags and removed several randomly placed closing tags .]

Re: syntax for map

Posted: 18 Mar 2024, 11:31
by teadrinker
sanneedshelp wrote:

Code: Select all

msgbox item
Unclear what should be displayed in the message box.

Re: syntax for map

Posted: 18 Mar 2024, 11:57
by flyingDman
I agree that the use of msgbox item is possibly due to a poor understanding of what maps are. I suggest for the OP to review the documentation about objects and maps in particular. (see https://www.autohotkey.com/docs/v2/Objects.htm).
Going out on a limb: if the intent is to create a map from the elements of a CSV (Comma Separated Values) string, where the odd elements are the keys and the even elements become the values , try this:

Code: Select all

str := 'value1,(answer1),value2,(answer2),value3,(answer3),value4,(answer4),value5,(answer5)'
mapvar := map()
for x,y in z := strsplit(str,",")	
	(mod(x,2) = 1) && mapvar[y] := z[a_index+1]
; testing:
for x,y in mapvar
	msgbox x "--->" y

Re: syntax for map

Posted: 18 Mar 2024, 12:03
by teadrinker
@flyingDman

Code: Select all

str := 'value1,(answer1),value2,(answer2),value3,(answer3),value4,(answer4),value5,(answer5)'
mapvar := Map(StrSplit(str, ',')*)
; testing:
for x,y in mapvar
	msgbox x "--->" y

Re: syntax for map

Posted: 18 Mar 2024, 12:08
by flyingDman
@teadrinker Was not aware of the use of * with maps. Thanks.

Re: syntax for map

Posted: 18 Mar 2024, 13:10
by teadrinker
If you define the toString() method for Map, it can be displayed in MsgBox:

Code: Select all

#Requires AutoHotkey v2.0

Map.Prototype.DefineProp('toString', {
    Call: (m, indent := '    ') => (
        res := ['{'], e := m.__Enum(2),
        [(
            (*) => e(&k, &v) && (
                res[1] .= (res[1] = '{' ? '' : ',') . '`n' . indent
                    (IsObject(k) ? Type(k) : '"' . k . '"')
                    ': '
                    (IsObject(v) ? Type(v) : '"' . v . '"')
            )
        )*],
        res[1] . '`n}'
    )
})

str := 'value1,(answer1),value2,(answer2),value3,(answer3),value4,(answer4),value5,(answer5)'
mapvar := Map(StrSplit(str, ',')*)
; testing:
MsgBox String(mapvar)

Re: syntax for map

Posted: 18 Mar 2024, 17:43
by FanaticGuru
flyingDman wrote:
18 Mar 2024, 12:08
@teadrinker Was not aware of the use of * with maps. Thanks.

It is more of * with any array in any function thing.

* just 'expands' an array and tells a function that elements are to be treated as separate parameters resulting in a variadic function call.

Code: Select all

Arr := ['StringThing', 7, 6]
Msgbox SubStr(Arr*)
; same as
Msgbox SubStr('StringThing', 7, 6)

There are also a few uses outside functions.

FG

Re: syntax for map

Posted: 18 Mar 2024, 18:36
by flyingDman
I have been using variadic functions for years. Just not aware that is could be applied with a map like this. Thank you.

Re: syntax for map

Posted: 18 Mar 2024, 18:46
by sanneedshelp
Sorry very new to coding and if question seems dumb please apologies and i'm still learning... Thanks for all replies


working code

Code: Select all

var := Map('value1', 'answer1','value2', 'answer2')
m    := Menu()
submenu := Menu()
For varname in var
submenu.Add varname, funcgo
m.Add 'Main menu', submenu
funcgo(varname, varPos, m) {
 A_Clipboard := '', A_Clipboard := var[varname]
  Send '^v'
}

#x::m.Show



I don't want to hardcode within the Code. it should be dynamically getting from else where for example an url .
I wanted to see what is being returned to item to debug further that's why asked how to display in msg box but i'm running into issues converting http response into map
was thinking to convert it to string and then to map but that did not work.


Not working code

Code: Select all

#Requires AutoHotkey v2.0

url := "https://ahkstorage.blob.core.windows.net/ran/ran.txt"  ; Replace with your URL
http := ComObject("WinHttp.WinHttpRequest.5.1")
http.Open("GET", url)
http.Send()
ranvar := http.ResponseText

item1 := string(ranvar)

item := Map(item1)
; msgbox string(item) 


m := Menu()
submenu := Menu()
For varname in item
 submenu.Add varname, funcgo
m.Add 'Main menu', submenu


funcgo(varname, itemPos, m) {
 A_Clipboard := '', A_Clipboard := item[varname]
  Send '^v'
}

#x::m.Show
DllCall("SwitchToThisWindow", "UInt", A_ScriptHwnd, "UInt", 1)


Re: syntax for map  Topic is solved

Posted: 18 Mar 2024, 20:48
by teadrinker

Code: Select all

#Requires AutoHotkey v2.0

url := "https://ahkstorage.blob.core.windows.net/ran/ran.txt"
http := ComObject("WinHttp.WinHttpRequest.5.1")
http.Open("GET", url, true)
http.Send()
http.WaitForResponse()

mapObj := Map(StrSplit(http.ResponseText, ",", " '")*)

m := Menu()
submenu := Menu()
for item in mapObj {
    submenu.Add(item, (itemName, *) => SendText(mapObj[itemName]))
}
m.Add("Main menu", submenu)

#x:: m.Show()

Re: syntax for map

Posted: 19 Mar 2024, 21:31
by sanneedshelp
@teadrinker Thank you very much sir!