if variable = value of group Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Scr1pter
Posts: 1205
Joined: 06 Aug 2017, 08:21
Location: Germany

if variable = value of group

13 Jun 2020, 17:35

Hello,

This code works, but I wonder if I can create a group which contains all allowed values,
so that the if statement checks if the value is an allowed one.

Code: Select all

F11::
variable = 3

if (variable = 4) or (variable = 3) or (variable = 2)
{
  MsgBox, correct
}
else
{
  MsgBox, incorrect
}
return
My idea is to create an array and let ahk loop through it.
However, I think I saw somewhere here a better solution.

Here some kind of PSEUDO code:

Code: Select all

F12::
allowedValues = [2, 3, 4}
variable = 3

if variable is in allowedValues
{
  MsgBox, correct
}
else
{
  MsgBox, incorrect
}
return
Is something like this possible?

P.S: I tried it with GroupAdd, but then I recognized that command was only made for WinTitles.

Thanks for any help and best regards
Please use [code][/code] when posting code!
Keyboard: Logitech G15 FE - Mice: Logitech G9 FE, G502 FE, G602, Jelly Comb Touchpad - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.32.00
Aceful
Posts: 6
Joined: 21 Apr 2020, 08:10

Re: if variable = value of group

13 Jun 2020, 17:43

You can do something like this

Code: Select all

F12::
allowedValues = 2,3,4
variable = 5

if variable contains %allowedValues%
  MsgBox, correct
else
  MsgBox, incorrect
return
braunbaer
Posts: 86
Joined: 22 Feb 2016, 10:49

Re: if variable = value of group

13 Jun 2020, 19:34

contains is wrong becaue in your example, 20 or 43 would be recognized as valid value. Use the in operator:

Code: Select all

if variable in %allowedValues%
User avatar
Smile_
Posts: 283
Joined: 03 May 2020, 00:51

Re: if variable = value of group

13 Jun 2020, 20:08

With array yeah possible, my idea here is to search inside the array, once the value is found, it stops otherwise it continue until end of the array length is reached, and then display the result message:

Code: Select all

F12::
allowedValues := [2, 3, 4]
variable := 3
Msg := "Incorrect"
Loop % allowedValues.Length()
if (variable = allowedValues[A_Index]) {
    Msg := "Correct"
	Break
}
Msgbox % Msg
Helgef
Posts: 4459
Joined: 17 Jul 2016, 01:02
Contact:

Re: if variable = value of group

14 Jun 2020, 05:34

You can also use switch,

Code: Select all

var := 1
switch var {
	case 1, 2, 3, 4: msgbox % "it is 1,2,3 or 4"
	default: msgbox % "it is something else"
}
Cheers
User avatar
Chunjee
Posts: 689
Joined: 18 Apr 2014, 19:05
GitHub: Chunjee

Re: if variable = value of group

14 Jun 2020, 06:39

.includes may be of use here.


Checks if value is in collection. If collection is a string, it's checked for a substring of value.

Code: Select all

A := new biga() ; requires https://www.npmjs.com/package/biga.ahk

allowedValues := [2, 3, 4]
A.includes(allowedValues, 3)
; => true

I think ahk should have indexOf built in
User avatar
Scr1pter
Posts: 1205
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: if variable = value of group  Topic is solved

15 Jun 2020, 16:06

Thank you ALL for your help!

Good to see my pseudo code was close to the solution.

I decided that choose this way (for now):

Code: Select all

F11::
#include Allowed Values.txt

if Clipboard in %allowedValues%
{
  MsgBox, correct
}
else
{
  MsgBox, incorrect
}
return
Thanks and best regards!
Please use [code][/code] when posting code!
Keyboard: Logitech G15 FE - Mice: Logitech G9 FE, G502 FE, G602, Jelly Comb Touchpad - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.32.00

Return to “Ask For Help”

Who is online

Users browsing this forum: AHKStudent, boiler, mikeyww and 62 guests