Reputation: 570
I'm just trying to perform a very simple for loop in a function and yet it's not working as expected. However, If I replace the checkForImage() call with the contents of the function, it works. Is there something wrong with my syntax?
dict := {"img1": "shark","img2": "blue","img3": "red"}
sendAnswers(answer) {
msgbox, %answer%
}
checkForImage() {
MsgBox hi
for key, val in dict
MsgBox %val%
return
}
^z::
checkForImage()
I get a message box with "hi" but the loop doesn't seem to do anything.
I am using Version 1.1.30.00
Upvotes: 0
Views: 163
Reputation: 166
Your syntax is correct. The function simply cannot "see" dict
. There are a couple of ways to solve this problem, see Global.
First method: Only dict
is global, all other variables are accessible only within checkForImage()
. If you're accessing only dict
or a couple more global variables, this method is recommended.
checkForImage() {
global dict ; Only dict will be global (accessible outside this function)
myLocalVariable := 0 ; This is accessible only within this function
MsgBox hi
for key, val in dict
MsgBox %val%
}
Second method: ALL variables within the function is global.
checkForImage() {
Global ; dict and all other global variables will be accessible
myNotLocalVariable := 0 ; This is accessible even outside this function
MsgBox hi
for key, val in dict
MsgBox %val%
return
}
Third method: Declare dict
as a super-global variable.
global dict := {"img1": "shark","img2": "blue","img3": "red"}
checkForImage() {
MsgBox hi
for key, val in dict
MsgBox %val%
return
}
Upvotes: 3