Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if string is contained in an array in AutoHotKey

I have following code:

ignored := [ "Rainmeter.exe", "Nimi Places.exe", "mumble.exe" ]

a := ignored.HasKey("mumble.exe")
MsgBox,,, %a%

It returns 0 even though the string is clearly present in the array.

How do I test if a string value is present in an array?

PS: I also tried if var in which gives same results.

like image 610
monnef Avatar asked Nov 08 '15 07:11

monnef


People also ask

How do you check if a string exists in an array?

You can use the includes() method in JavaScript to check if an item exists in an array. You can also use it to check if a substring exists within a string. It returns true if the item is found in the array/string and false if the item doesn't exist.

How do I check if an array contains something?

The includes() method returns true if an array contains a specified value. The includes() method returns false if the value is not found.

How can you tell if something is in an array TypeScript?

To check if a value is an array of specific type in TypeScript: Use the Array. isArray() method to check if the value is an array. Iterate over the array and check if each value is of the specific type.

How do you check if there is an element in an array C#?

To check if an array contains a specific element in C#, call Array. Exists() method and pass the array and the predicate that the element is specified element as arguments. If the element is present in the array, Array. Exists() returns true, else it returns false.


1 Answers

You can't, using just one command. Such functionality is not implemented in AHK_L as of 1.1.22.3.

You'll have to either define your own function

hasValue(haystack, needle) {
    if(!isObject(haystack))
        return false
    if(haystack.Length()==0)
        return false
    for k,v in haystack
        if(v==needle)
            return true
    return false
}

or use some fancy workaround:

ignored := { "Rainmeter.exe":0, "Nimi Places.exe":0, "mumble.exe":0 }
msgbox, % ignored.HasKey("mumble.exe")

This would create an associative array and put your values as keys (the values are set to 0 here), so the .HasKey() makes sense to use.

like image 135
phil294 Avatar answered Sep 23 '22 17:09

phil294