Jump to content

Recommended Posts

Posted (edited)
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Version=Beta ; not needed
#AutoIt3Wrapper_UseX64=y ; not needed
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
Exit Testing() ; https://www.autoitscript.com/forum/topic/213038-ternary-operation-incongruencies/#comment-1544855
Func Testing()
    Local $sVar, $sReturn, $iMatch1 = 0, $iMatch2 = 1
    For $n = 0 To 2
;~      ConsoleWrite(@CRLF & @CRLF & '+' & $n & @CRLF)
        For $i = 0 To 1
            If $i Then $iMatch1 = Int(Not $iMatch1)
            If $i Then $iMatch2 = Int(Not $iMatch2)
            $sVar = "0" & $n
;~          ConsoleWrite(@CRLF & "--- " & $sVar & ", $iMatch1: " & $iMatch1 & ', $iMatch2: ' & $iMatch2 & ', $sVar: ' & $sVar & @CRLF)


            $sReturn = ((Int($sVar) = $iMatch1 Or Int($sVar) = $iMatch2) ? "ABC" : "123") ; flawless ; it is strongly recommended that the 3 elements are enclosed in parentheses.
            ConsoleWriteValues($sReturn, $sVar & ',' & $iMatch1 & ',' & $iMatch2, " expected to return a string - works as expected")

            $sReturn = ((Int($sVar) = $iMatch1 Or Int($sVar) = $iMatch2) ? "ABC" : "123") & " ( and a string )"  ; flawless ; it is strongly recommended that the 3 elements are enclosed in parentheses.
            ConsoleWriteValues($sReturn, $sVar & ',' & $iMatch1 & ',' & $iMatch2, " expected to return a string and an additional string  - works as expected")


            $sReturn = (Int($sVar) = $iMatch1 Or Int($sVar) = $iMatch2) ? "ABC" : "123"
            ConsoleWriteValues($sReturn, $sVar & ',' & $iMatch1 & ',' & $iMatch2, " expected to return a string - works as expected")

            $sReturn = (Int($sVar) = $iMatch1 Or Int($sVar) = $iMatch2) ? "ABC" : "123" & " ( and a string )"
            ConsoleWriteValues($sReturn, $sVar & ',' & $iMatch1 & ',' & $iMatch2, " expected to return a string and an additional string - works as expected")


            $sReturn = (Int($sVar) = $iMatch1 Or Int($sVar) = $iMatch2 ? "ABC" : "123") ; it is strongly recommended that the 3 elements are enclosed in parentheses.
            ConsoleWriteValues($sReturn, $sVar & ',' & $iMatch1 & ',' & $iMatch2, " expected to return a string - wrong, needs attention")

            $sReturn = (Int($sVar) = $iMatch1 Or Int($sVar) = $iMatch2 ? "ABC" : "123") & " ( and a string )" ; it is strongly recommended that the 3 elements are enclosed in parentheses.
            ConsoleWriteValues($sReturn, $sVar & ',' & $iMatch1 & ',' & $iMatch2, " expected to return a string and an additional string - wrong, needs attention")


            $sReturn = Int($sVar) = $iMatch1 Or Int($sVar) = $iMatch2 ? "ABC" : "123"
            ConsoleWriteValues($sReturn, $sVar & ',' & $iMatch1 & ',' & $iMatch2, " expected to return a string - wrong, needs attention")

            $sReturn = Int($sVar) = $iMatch1 Or Int($sVar) = $iMatch2 ? "ABC" : "123" & " ( and a string )"
            ConsoleWriteValues($sReturn, $sVar & ',' & $iMatch1 & ',' & $iMatch2, " expected to return a string and an additional string - wrong, needs attention")

        Next
    Next
    ConsoleWrite(@CRLF & @CRLF)
    ConsoleWriteValues()
EndFunc   ;==>Testing

Func ConsoleWriteValues($vVal = Default, $sVals = "", $sDesc = "", $iLine = @ScriptLineNumber, $iError = @error, $iExtended = @extended)
    Local Static $aAll[1][2], $sShow
    If $vVal = Default Then
        ConsoleWrite(@CRLF & "--- --- --- --- to sum it all up --- --- --- --- --- " & @CRLF & @CRLF)
        For $n = 1 To UBound($aAll) - 1
            If $aAll[$n][0] = "" Then ContinueLoop
            ConsoleWrite(@CRLF & '-- at line: ' & $n & " (" & $aAll[$n][1] & ")" & $aAll[$n][0] & @CRLF)
        Next
        ConsoleWrite(@CRLF)
    Else
        $sShow = @CRLF & "@@(" & $iLine - 1 & ") : @error: " & $iError & ',' & $iExtended & @TAB & _
                " VarType: " & StringLeft(VarGetType($vVal) & '    ', 6) & @TAB & 'Vals: ' & $sVals &  @TAB & "Value: " & $vVal ; "Binary: " & StringToBinary($vVal) & @TAB &
        If $iLine > UBound($aAll) Then ReDim $aAll[$iLine + 10][2]
        $aAll[$iLine - 1][1] = $sDesc
        $aAll[$iLine - 1][0] &= $sShow
;~      ConsoleWrite($sShow) ; not an useful output
    EndIf

EndFunc   ;==>ConsoleWriteValues

renders:
 

--- --- --- --- to sum it all up --- --- --- --- --- 


-- at line: 17 ( expected to return a string - works as expected)
@@(17) : @error: 0,0     VarType: String    Vals: 00,0,1    Value: ABC
@@(17) : @error: 0,0     VarType: String    Vals: 00,1,0    Value: ABC
@@(17) : @error: 0,0     VarType: String    Vals: 01,1,0    Value: ABC
@@(17) : @error: 0,0     VarType: String    Vals: 01,0,1    Value: ABC
@@(17) : @error: 0,0     VarType: String    Vals: 02,0,1    Value: 123
@@(17) : @error: 0,0     VarType: String    Vals: 02,1,0    Value: 123

-- at line: 20 ( expected to return a string and an additional string  - works as expected)
@@(20) : @error: 0,0     VarType: String    Vals: 00,0,1    Value: ABC ( and a string )
@@(20) : @error: 0,0     VarType: String    Vals: 00,1,0    Value: ABC ( and a string )
@@(20) : @error: 0,0     VarType: String    Vals: 01,1,0    Value: ABC ( and a string )
@@(20) : @error: 0,0     VarType: String    Vals: 01,0,1    Value: ABC ( and a string )
@@(20) : @error: 0,0     VarType: String    Vals: 02,0,1    Value: 123 ( and a string )
@@(20) : @error: 0,0     VarType: String    Vals: 02,1,0    Value: 123 ( and a string )

-- at line: 24 ( expected to return a string - works as expected)
@@(24) : @error: 0,0     VarType: String    Vals: 00,0,1    Value: ABC
@@(24) : @error: 0,0     VarType: String    Vals: 00,1,0    Value: ABC
@@(24) : @error: 0,0     VarType: String    Vals: 01,1,0    Value: ABC
@@(24) : @error: 0,0     VarType: String    Vals: 01,0,1    Value: ABC
@@(24) : @error: 0,0     VarType: String    Vals: 02,0,1    Value: 123
@@(24) : @error: 0,0     VarType: String    Vals: 02,1,0    Value: 123

-- at line: 27 ( expected to return a string and an additional string - works as expected)
@@(27) : @error: 0,0     VarType: String    Vals: 00,0,1    Value: ABC
@@(27) : @error: 0,0     VarType: String    Vals: 00,1,0    Value: ABC
@@(27) : @error: 0,0     VarType: String    Vals: 01,1,0    Value: ABC
@@(27) : @error: 0,0     VarType: String    Vals: 01,0,1    Value: ABC
@@(27) : @error: 0,0     VarType: String    Vals: 02,0,1    Value: 123 ( and a string )
@@(27) : @error: 0,0     VarType: String    Vals: 02,1,0    Value: 123 ( and a string )

-- at line: 31 ( expected to return a string - wrong, needs attention)
@@(31) : @error: 0,0     VarType: Bool      Vals: 00,0,1    Value: True
@@(31) : @error: 0,0     VarType: String    Vals: 00,1,0    Value: ABC
@@(31) : @error: 0,0     VarType: Bool      Vals: 01,1,0    Value: True
@@(31) : @error: 0,0     VarType: String    Vals: 01,0,1    Value: ABC
@@(31) : @error: 0,0     VarType: String    Vals: 02,0,1    Value: 123
@@(31) : @error: 0,0     VarType: String    Vals: 02,1,0    Value: 123

-- at line: 34 ( expected to return a string and an additional string - wrong, needs attention)
@@(34) : @error: 0,0     VarType: String    Vals: 00,0,1    Value: True ( and a string )
@@(34) : @error: 0,0     VarType: String    Vals: 00,1,0    Value: ABC ( and a string )
@@(34) : @error: 0,0     VarType: String    Vals: 01,1,0    Value: True ( and a string )
@@(34) : @error: 0,0     VarType: String    Vals: 01,0,1    Value: ABC ( and a string )
@@(34) : @error: 0,0     VarType: String    Vals: 02,0,1    Value: 123 ( and a string )
@@(34) : @error: 0,0     VarType: String    Vals: 02,1,0    Value: 123 ( and a string )

-- at line: 38 ( expected to return a string - wrong, needs attention)
@@(38) : @error: 0,0     VarType: Bool      Vals: 00,0,1    Value: True
@@(38) : @error: 0,0     VarType: String    Vals: 00,1,0    Value: ABC
@@(38) : @error: 0,0     VarType: Bool      Vals: 01,1,0    Value: True
@@(38) : @error: 0,0     VarType: String    Vals: 01,0,1    Value: ABC
@@(38) : @error: 0,0     VarType: String    Vals: 02,0,1    Value: 123
@@(38) : @error: 0,0     VarType: String    Vals: 02,1,0    Value: 123

-- at line: 41 ( expected to return a string and an additional string - wrong, needs attention)
@@(41) : @error: 0,0     VarType: Bool      Vals: 00,0,1    Value: True
@@(41) : @error: 0,0     VarType: String    Vals: 00,1,0    Value: ABC
@@(41) : @error: 0,0     VarType: Bool      Vals: 01,1,0    Value: True
@@(41) : @error: 0,0     VarType: String    Vals: 01,0,1    Value: ABC
@@(41) : @error: 0,0     VarType: String    Vals: 02,0,1    Value: 123 ( and a string )
@@(41) : @error: 0,0     VarType: String    Vals: 02,1,0    Value: 123 ( and a string )

@jpm, would you take a look at this. Am not pushing this to trac because I may be wrong in my thinking so, here to clarify/evaluate.

Anyone seeing a flaw in my trend of thought please post.
The 1st option with each properly enclosed in parentheses is flawless and solves the "problem" but, maybe it could/should be looked at in the stub level ?

Edit: fixed oops in demo

Edited by argumentum
oops

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Posted (edited)
Global $a = Int("0" & 1)
Global $b = False

ConsoleWrite("1: ")
ConsoleWrite($a Or $b ? "ABC" : "123") ;expected "ABC" but result is true
ConsoleWrite(@CRLF & "2: ")
ConsoleWrite(($a Or $b) ? "ABC" : "123") ;expected "ABC" and result is ABC
ConsoleWrite(@CRLF & "3: ")
ConsoleWrite($a Or ($b ? "ABC" : "123")) ;expected True and result is True
ConsoleWrite(@CRLF)

ConsoleWrite("4: ")
ConsoleWrite($a And $b ? "ABC" : "123") ;expected "123" and result is 123
ConsoleWrite(@CRLF & "5: ")
ConsoleWrite(($a And $b) ? "ABC" : "123") ;expected "123" and result is 123
ConsoleWrite(@CRLF & "6: ")
ConsoleWrite($a And ($b ? "ABC" : "123")) ;expected True and result is True
ConsoleWrite(@CRLF)

I assume ternary operation has higher prio than the logical operators for "or" if you don't use brackets.

$a = "FALSE" is always TRUE because it is a non empty string.

1.) Is $b = False Then result is "123" in the ternary operation which is True, too.

2) $a or True (from 1.) -> True

Output

1: True
2: ABC
3: True
4: 123
5: 123
6: True

If you mean this.

Imho, logical operators should have higher prio then ternary operation. Maybe a bug here.

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Posted (edited)
2 hours ago, UEZ said:

I assume ternary operation has higher prio than the logical operators for "or" if you don't use brackets.

Maybe it's not only for Or, but also for And, as in the last tests 16a/16b shown below ?
Here are a few tests based on UEZ test1 :

Global $b = 100

ConsoleWrite("11 : ")
ConsoleWrite(0 Or $b = 100) ; result is True

ConsoleWrite(@CRLF & "12 : ")
ConsoleWrite(0 Or $b = 0) ; result is False

ConsoleWrite(@CRLF & "13 : ")
ConsoleWrite(0 Or "ABC") ;  result is True

;===========================
ConsoleWrite(@CRLF & "14 : ")
ConsoleWrite(444 Or $b ? "ABC" : "123") ;  result is True (444 on the left side of Or is evaluated first => True)

ConsoleWrite(@CRLF & "14a: ")
ConsoleWrite(0 Or $b ? "ABC" : "123") ; result is "ABC" <======== ternary operator got a higher priority over OR when no brackets

ConsoleWrite(@CRLF & "14b: ")
ConsoleWrite(0 Or ($b ? "ABC" : "123")) ; result is True (when brackets surround ternary, as they always should)
;===========================

;///////////////////////////
ConsoleWrite(@CRLF & "15a: ")
ConsoleWrite($b ? "ABC" : "123" Or 444) ; result is "ABC" <======== ternary operator got a higher priority over OR when no brackets

ConsoleWrite(@CRLF & "15b: ")
ConsoleWrite(($b ? "ABC" : "123") Or 444) ; result is True (when brackets surround ternary, as they always should)
;///////////////////////////

ConsoleWrite(@CRLF & "16a: ")
ConsoleWrite(444 AND $b ? "ABC" : "123") ; result is "ABC" <======== ternary operator got a higher priority over AND when no brackets

ConsoleWrite(@CRLF & "16b: ")
ConsoleWrite(444 AND ($b ? "ABC" : "123")) ; result is True (when brackets surround ternary, as they always should)

ConsoleWrite(@CRLF)

Output:

11 : True
12 : False
13 : True
14 : True
14a: ABC
14b: True
15a: ABC
15b: True
16a: ABC
16b: True

Anyway, the help file stipulates what follows (topic Ternary) which solves all cases :

Although not necessary in all cases, it is strongly recommended that the 3 elements are enclosed in parentheses.

For users who are too lazy to surround the 3 elements with parentheses (that's me !) please always surround at least the whole ternary part with a parenthese, like in tests 14b/15b/16b

Edited by pixelsearch
typo

"I think you are searching a bug where there is no bug... don't listen to bad advice."

Posted (edited)

The way I see it, it'd be a simple fix. Fortunately with proper enclosure it behaves. Nonetheless it deserves attention if there is time for it.
If there's no time for it then amend the help file, to enclose all evaluations greater than a single one, with an extra example showing how.

Updated the 1st post to an example/demo that is simpler to observe.

Edited by argumentum

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Posted

I did not read fully everything in here, but just for the discussion case, AutoIt will stop after an And if the first part is False and will stop after an Or if the first part is True.  So maybe this is why you do not get the expected result.

Posted

If one codes carefully, you're right. But where can you "2" + 2 return 4, or "2" & 2 return 22. Welcome to AutoIt :)
The manual say "it is strongly recommended that the 3 elements are enclosed in parentheses." and, "blessed be" those that read it :D

If you have a floating ?(question mark), then is a Ternary operation, and based on that the rest can be corrected internally.
But if there's no time for such, amend the help file because there is no mention of it. And yes, the help file is "a learn to code tool" too 🤷‍♂️

And to be clear, there is no drama. Is not an "OMG a bug !", because I'd tell the user that he/she is the bug and go learn to code you :censored: !!!  :lol:

But the circumstance could use attention.

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...