If Expression check to see if value is in Array

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
PaulLeavitt
Posts: 58
Joined: 21 Oct 2014, 10:04

If Expression check to see if value is in Array

14 Sep 2015, 15:24

Hey all! So I'm working on an if expression that works off of a dropdown input. Basically. if the user selects an option from the dropdown, I want the script to check to see if the value is in an array or object or something. If it is, than do option A, if it's not, do option B. The way I've been doing this is just to type out the whole thing every time. For example:

Code: Select all

If (CityChoice = "Phoenix") or (CityChoice = "Portland") or (CityChoice = "Inland Empire") or (CityChoice = "Sacramento")
What I'd love to be able to do would be something sort of like this:

Code: Select all

CityArray: (Phoenix|Portland|Inland Empire|Sacramento)

If CityChoice = (CheckArray = cityArray)
{
do stuff
}
Is something like this possible? I've not used array's or objects much, but I'm pretty good with AHK.
Thanks!
Paul
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: If Expression check to see if value is in Array

14 Sep 2015, 15:34

Code: Select all

CityArray := {Phoenix: 1, Portland: 1, "Inland Empire": 1, Sacramento: 1}
 
If (cityArray[CityChoice])
{
  ;do stuff
}
User avatar
PaulLeavitt
Posts: 58
Joined: 21 Oct 2014, 10:04

Re: If Expression check to see if value is in Array

14 Sep 2015, 15:49

Ah! You rock Kon!
That's awesome! So is that an object or an Array? (basically wondering where I should go to do more study)
One last question I realized I didn't specify, how do I declare the contents of an array to be the options in a dropdown?
Thanks!
Paul
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: If Expression check to see if value is in Array

14 Sep 2015, 17:36

Take a look at Objects and Arrays. On the "Objects" page, at first you will mostly just need to know the info in the first couple sections. This is very important:
Objects wrote:An object reference is a pointer or "handle" to a particular object. Like strings and numbers, object references can be stored in variables, passed to or returned from functions and stored in objects. After copying a reference from one variable to another as in x := y, both variables refer to the same object.
Object shows the built in methods a standard AHK object has. Methods are like functions, except they belong to an object and they do something to their object when called.

In AHK, "Arrays" are the same thing as objects. We commonly refer to simple objects as arrays, but all arrays are objects.

To add all the keys from an array into a DDL, you will need to enumerate the object and collect each key:

Code: Select all

CityArray := {Phoenix: 1, Portland: 1, "Inland Empire": 1, Sacramento: 1}
MyDDL := ""
for key, val in CityArray ; Note, this will enumerate keys in alphabetical order
  MyDDL .= key "|" (A_Index = 1 ? "|" : "")
Gui, Add, DropDownList,, %MyDDL%
Gui, Show
return

GuiClose:
ExitApp
User avatar
PaulLeavitt
Posts: 58
Joined: 21 Oct 2014, 10:04

Re: If Expression check to see if value is in Array

17 Sep 2015, 15:09

Ah, I see! I'll definitely be doing some more reading! Thanks so much Kon!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], peter_ahk and 376 guests