Page 1 of 1

if variable = value of group

Posted: 13 Jun 2020, 17:35
by Scr1pter
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

Re: if variable = value of group

Posted: 13 Jun 2020, 17:43
by Aceful
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

Re: if variable = value of group

Posted: 13 Jun 2020, 19:34
by braunbaer
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%

Re: if variable = value of group

Posted: 13 Jun 2020, 20:08
by Smile_
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

Re: if variable = value of group

Posted: 14 Jun 2020, 05:34
by Helgef
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

Re: if variable = value of group

Posted: 14 Jun 2020, 06:39
by Chunjee
.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

Re: if variable = value of group  Topic is solved

Posted: 15 Jun 2020, 16:06
by Scr1pter
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!