syntax for map Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
sanneedshelp
Posts: 18
Joined: 10 Mar 2024, 19:52

syntax for map

18 Mar 2024, 10:59

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 .]
teadrinker
Posts: 4368
Joined: 29 Mar 2015, 09:41
Contact:

Re: syntax for map

18 Mar 2024, 11:31

sanneedshelp wrote:

Code: Select all

msgbox item
Unclear what should be displayed in the message box.
User avatar
flyingDman
Posts: 2832
Joined: 29 Sep 2013, 19:01

Re: syntax for map

18 Mar 2024, 11:57

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
14.3 & 1.3.7
teadrinker
Posts: 4368
Joined: 29 Mar 2015, 09:41
Contact:

Re: syntax for map

18 Mar 2024, 12:03

@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
User avatar
flyingDman
Posts: 2832
Joined: 29 Sep 2013, 19:01

Re: syntax for map

18 Mar 2024, 12:08

@teadrinker Was not aware of the use of * with maps. Thanks.
14.3 & 1.3.7
teadrinker
Posts: 4368
Joined: 29 Mar 2015, 09:41
Contact:

Re: syntax for map

18 Mar 2024, 13:10

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)
User avatar
FanaticGuru
Posts: 1907
Joined: 30 Sep 2013, 22:25

Re: syntax for map

18 Mar 2024, 17:43

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
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
User avatar
flyingDman
Posts: 2832
Joined: 29 Sep 2013, 19:01

Re: syntax for map

18 Mar 2024, 18:36

I have been using variadic functions for years. Just not aware that is could be applied with a map like this. Thank you.
14.3 & 1.3.7
sanneedshelp
Posts: 18
Joined: 10 Mar 2024, 19:52

Re: syntax for map

18 Mar 2024, 18:46

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)

teadrinker
Posts: 4368
Joined: 29 Mar 2015, 09:41
Contact:

Re: syntax for map  Topic is solved

18 Mar 2024, 20:48

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()
sanneedshelp
Posts: 18
Joined: 10 Mar 2024, 19:52

Re: syntax for map

19 Mar 2024, 21:31

@teadrinker Thank you very much sir!

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: No registered users and 20 guests