Why doesn't my loop trigger inside a function? - autohotkey

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

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
}

Related

Is it possible to use a string variable in if condition in Swift?

I'm new to iOS development and wondering if I could pass a string variable inside if statement? Here's my pseudo code:
x = 1
func myFunc() -> String {
myString = "x == 1"
return myString
}
if(myfunc()) {
code i want to execute
}
I am currently getting the following error: "'String' is not convertible to 'Bool'"
Is there a way I can do this?
You should use a comparison operator for this.
if myString == myFunc() {
// your code
}
If statement always wants a condition that can return a bool value. i.e. true and false.
In your above code, you are not providing sufficient data to if statement so that it can calculate whether the result iss true or false.
When you compare it like if myString == myFunc() , if statement will compare the string and return true if string matches else false.
if the string matches, it will execute the code that is within if conditions scope. Otherwise it will calculate the else condition.
UPDATE1:
I see you have updated the question, so you want to check if myFunc() is empty or not?
For that you can compare it with empty string.
if myFunc() == "" {
// your code
}
UPDATE2:
Question: (asked in comment) instead of writing "if(x == 1)" i am trying to use a variable so my if statement is "if(stringVaraible)" where stringVariable = "x ==1". Basically I am asking if it is possible to turn a string into normal code
Answer: No, you can't do that. Swift is a compiled language, not interpreted like Ajax. Read more here: https://stackoverflow.com/a/30058875/8374890
It's very specific and clear that you can't use String as boolean. The approach you can take is well known like..
if(myString == "x == 1") {
code i want to execute
}
or
if(desiredString == myFunc()) {
code i want to execute
}

Print variable name in Swift

I have a global variable like:
let myVar:Bool = true
I want to get the actual name (as String) of the variable. Something like:
print(myVar.name)
should print the myVar string.
Note: I found this solution but it does not deal with global variables. It will only work with classes & structs it seems.
Thanks!

Why outside of block swift can't see value assigned to a uninitialized variable in the block?

What is the mechanism of declaring w/o value in Swift5 ? Does the first assign become the real declaration ?
And, should we avoid to declare without value in Swift?
var v:String;
if true {
v = "Hello"
print(v) // print "Hello" when without the print below
}
print(v) // variable 'v' used before being initialized
var v:String="";
if true {
v = "Hello"
print(v) // print "Hello"
}
print(v) // print "Hello"
Well, the message is not very helpful, and that's the problem. This pattern (which I call computed initialization) is perfectly legal and useful and — amazingly — you can even use let instead of var. But you must initialize the uninitialized variable by all possible paths before you use it. So you have:
var v:String
if true {
v = "Hello"
}
print(v) // error
But hold my beer and watch this:
var v:String
if true {
v = "Hello"
} else {
v = "Goodbye"
}
print(v) // fine!
Or even:
let v:String
if true {
v = "Hello"
} else {
v = "Goodbye"
}
print(v) // fine!
Amazing, eh?
Now, you might say: OK, but true will always be true so it's silly to make me fulfill the "all paths" rule. Too bad! The compiler insists anyway, and then lets you off later with a warning that the else won't be executed. But a warning lets you compile; an error doesn't. The truth is that your example is very artificial. But this is a real-life possibility:
let v:String
if self.someBoolProperty {
v = "Hello"
} else {
v = "Goodbye"
}
print(v) // fine!
Not only is this sort of thing legal, it is actually the pattern that Apple recommends under certain slightly tricky circumstances. For instance, it is used in Apple's own example code showing how to use the Swift 5 Result struct:
let result: Result<Int, EntropyError>
if count < AsyncRandomGenerator.entropyLimit {
// Produce numbers until reaching the entropy limit.
result = .success(Int.random(in: 1...100))
} else {
// Supply a failure reason when the caller hits the limit.
result = .failure(.entropyDepleted)
}
So this is because swift compiles your code and notices that your value var v:String; is undeclared before being used which makes it an "optional" value. Even though you are assigning it within the if statement, if you were to get rid of the true value it is possible that the if statement would never run therefore no value will ever be stored in v, thus it would be used before "assigned".
So to answer your question if you want your value to be an optional and possible empty value declare v as the following var v:String? if you would like it to be a non-optional value with a value always stored within v you should declare it as the following var v = "". Swift will interpret this declaration as a String.
To answer your second question, defining without values in swift is 100% allowed, it really just depends on how you want to handle your code. I use optional values in my code all the time, but I don't necessarily like optionals so i usually like to declare my values such as var v = "" that way if the value is empty my UI or whatever else i'm manipulating won't break. But if i need to ensure a value is present i will have to make my value optional so i can use an if statement to check whether it's a valid value or not.
Shorter version of what I'm trying to say is, you are receiving the compiler warning because you are declaring v as a non-optional value rather than an optional value.

How use shortkey as variable instead text

Here my code:
key0 = {1}
key1 = {A}
Loop
{
If (GetKeyState(key0, "P"))
{
Send {key1}
}
else
{
Send {key0}
}
sleep 500
}
F12::ExitApp
Not works correctly. Cannot use keys as variables. Can anyone explain to me how to do this?
Correct syntax in the send command would be with % sign around the variable. Also you have figure brace in declaration {A} - should be just A if you use {A} literal, then it is not possible to do e.g. {a down}.
E.g. you pass key0 to Getkeystate() - the first argument there must be just key name unlike the curly braces in the send command.
Try this:
key0 = 1
key1 = A
loop
{
if getkeystate(key0, "P")
{
send {%key1%}
}
}

Argument Processing in golang

I will be giving the following as the command to run my go program.
go run app.go 3001-3005
What this is supposed to do is, run my go Rest api on server ports 3001 to 3005.
This part of my main function which handles this argument.
func main() {
ipfile := os.Args[1:]
s := strings.Split(ipfile, "-")
mux := routes.New()
mux.Put("/:key1/:value1", PutData)
mux.Get("/profile/:key1", GetSingleData)
mux.Get("/profile", GetData)
http.Handle("/", mix)
Here I will run a for loop and replace the first argument with s[i].
http.ListenAndServe(":3000", nil)
}
I get the following output:
cannot use ipfile (type []string) as type string in argument to strings.Split
What datatype does os.args return?
I tried converting it to string and then splitting. Does not work.
Please let me know what is wrong?
Like the error says, ipfile is a []string. The [1:] slice operation is going to return a slice, even if there's only 1 element.
After checking that os.Args has the enough elements, use:
ipfile := os.Args[1]
s := strings.Split(ipfile, "-")