Double IF statement always flags True

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
silvanpierce
Posts: 12
Joined: 23 Feb 2022, 12:41

Double IF statement always flags True

26 Apr 2024, 06:59

i am trying to use a series of if statements to check if variables exist & match. the problem is that the 2nd statement is always flagging true; causing the last portion of the script to trigger when it shouldn't.
in the code below, the loop will end up flagging 2 Vars as MW or OS. the first loop should trigger the final action, but the second one returns a messagebox of "462518" which is NOT in my "alrobranches" array. this means it should not trigger a final message box, but it does. i am completely stumped.

Code: Select all

Vars := ["br1p1","br2p1","br3p1","br4p1","br5p1","br6p1","br7p1","br1p2","br2p2","br3p2","br4p2","br5p2","br6p2","br7p2","br1p3","br2p3","br3p3","br4p3","br5p3","br6p3","br7p3","br1p4","br2p4","br3p4","br4p4","br5p4","br6p4","br7p4","br1p5","br2p5","br3p5","br4p5","br5p5","br6p5","br7p5","br1p6","br2p6","br3p6","br4p6","br5p6","br6p6","br7p6","br1p7","br2p7","br3p7","br4p7","br5p7","br6p7","br7p7","br1p8","br2p8","br3p8","br4p8","br5p8","br6p8","br7p8","br1p9","br2p9","br3p9","br4p9","br5p9","br6p9","br7p9"]

TotalResultP := 0
Result := ""
MW_Flag := 0
OS_Flag := 0

Loop, % Vars.MaxIndex()
{
alrobranches := [891400,891700,	888071,	888078,	888200,	891300,	888800,	889210,	888020,	889110,	888770,	890300,	890100,	888024,	888009,	888076, 888104,	891505,	891500,	891200,	889201,	888030,	889790,	889153,	889100,	889800,	890200,	888000,	889600,	891600,	889121,	890800,	890600,	890400,	888301,	888600,	889750,	889744,	891710,	891650,	889725,	889027,	888008,	889610,	888702,	888015,	889620,	888072,	889780, 0]

    VarName := Vars[A_Index]
    VarValue := %VarName%
    

    
    if (VarValue = "MW" or VarValue = "OS")
    {
        ResultP := VarName "p"
        ResultVen := VarName "ven"
        Result .= VarValue " has " %ResultP% "picks`n"
        Result .= VarValue " Vendor is " %ResultVen% "`n"
        varvar := %ResultP%
	venven := %ResultVen%
        ; Add the value of ResultP to the running total
        TotalResultP += %varvar%
77 := %resultven%
    if (VarValue = "MW" && (77 in alrobranches))
    {
        MW_Flag := 1
msgbox, %77%

    }
    
    if (VarValue = "OS" && (77 in alrobranches))
    {
        OS_Flag := 1
msgbox, %77%
    }
    }
}

if (TotalResultP >= 15 && (MW_Flag=1 && (OS_Flag=1)))
{
    Result .= "Total ResultP is " TotalResultP "`n"
    msgbox, final action
}

User avatar
mikeyww
Posts: 27130
Joined: 09 Sep 2014, 18:38

Re: Double IF statement always flags True

26 Apr 2024, 08:55

Hello,

The "in" syntax would not be used with an array. You would need to loop through the array elements to find a value.

Code: Select all

#Requires AutoHotkey v1.1.33.11
arr   := [1, 5]
found := False
For k, v in arr
 If (v = 5)
  found := True
MsgBox, 64, Found, % found
silvanpierce
Posts: 12
Joined: 23 Feb 2022, 12:41

Re: Double IF statement always flags True

26 Apr 2024, 09:37

mikeyww wrote:
26 Apr 2024, 08:55

arr := [1, 5]
@mikeyww does it matter in this case if my array items have quotes around them? i am checking to see if a 6 digit string (%77%) exists in my array of ~30, 6 digit strings
User avatar
mikeyww
Posts: 27130
Joined: 09 Sep 2014, 18:38

Re: Double IF statement always flags True

26 Apr 2024, 13:21

Try it and see! Let everyone know! :)
CoffeeChaton
Posts: 2
Joined: Yesterday, 10:50

Re: Double IF statement always flags True

Yesterday, 11:31

The operators "in" and "contains" are not supported in expressions. from https://www.autohotkey.com/docs/v1/lib/IfIn.htm#Remarks
if you need if ... in
you need to know, If (Expression) https://www.autohotkey.com/docs/v1/lib/IfExpression.htm
and if ... in https://www.autohotkey.com/docs/v1/lib/IfIn.htm
cannot appear at the same time

you can try it

Code: Select all


MsgBox, % ifin("A", "A,B,C") ; MsgBox 1
MsgBox, % ifin2("A", ["A", "B", "C"]) ; MsgBox 1

/**
* @param {string} Var like `A`
* @param {string} MatchList like `A,B,C`
* @example
* ```ahk
*   MsgBox % ifin("A", "A,B,C")
* ```
*/
ifin(Var, MatchList) {
    If Var in %MatchList%
        Return True
    Return False
}

/**
* @param {string} Var like `A`
* @param {string[]} MatchList like `["A","B","C"]`
* @example
* ```ahk
*   MsgBox % ifin("A", ["A","B","C"])
* ```
*/
ifin2(Var, MatchList) {
    result := ""
    For i, Value in MatchList {
        result .= Value (i < MatchList.Length() ? "," : "")
    }

    If Var in %result%
        Return True
    Return False
}


Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: GEOVAN, mikeyww and 143 guests