Jump to content

Recommended Posts

Posted

Hello All... I have been using AutoIt for some time now. Years actually... As of just a short

time ago I found out... via chatting with a very helpful user... that AutoIt can work with

Ternary Operators... I was looking over his script and came across the following line...

$sMsg = ($bMute = 1) ? ('muted!') : ('not muted!')

So... I was like... What?... how is this working...

He pointed me to the info I needed in AutoIt Help...

I am not sure if maybe there are others that did not

know this either... so I wrote a small script that not 

only helped me to understand it better... it just may

help someone else... either way... its attached...

Any Feedback would be much appreciated...

TernaryOpV100.au3

  • Solution
Posted

I have put it in SciTE Abbreviation Manager as iif (from the homonyms command in vba)
so when I type iif it converts to:

(expression ? if_expression_is_True : if_expression_is_False)


Here is a clamp example with a double tenary.

Local $iNum = 110

;min=0 ; max=100
$iNum = ($iNum < 0 ? 0 : ($iNum > 100 ? 100 : $iNum))
ConsoleWrite("$iNum=" & $iNum & @CRLF)

 

Tip:

Spoiler

You could put your code directly in your post, instead as downloaded file.
would make it more readable and reader-friendly

#cs ---------------------------------------------------------------------------

Ternary Operator Info:
 
? : Select the conditionally of an expression!

Example: $condition ? $expression1 : $expression2

($expression1 if $condition is True or $expression2 if False)

#ce ---------------------------------------------------------------------------

#include <MsgBoxConstants.au3>
; ---------------------------------------
$MsgBoxText = 0
$ValueSet01 = 0
$PrgSetVers = "v1.00"
$PrgSetName = "TernaryOp"
$PrgNameVer = $PrgSetName&" "&$PrgSetVers
; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
; -----------------------------------------------------------------------------
; Note: Changing $ValueSet01 to any other Num will make: Condition = 'NOT TRUE'
; -----------------------------------------------------------------------------

$ValueSet01 = 0 ;-----------------------------------------> Value Set (Var#:01)

$MsgBoxText = ($ValueSet01 = 0) ? ("TRUE") : ("NOT TRUE")

$MsgBoxText = "Found that the Condition of $ValueSet01 = "&$MsgBoxText&"!"

MsgBox(262208, $PrgNameVer&" - Condition Detected!", $MsgBoxText, 90)

Exit ;----------------------------------------------------> End Of Script/Exit!

 

 

I know that I know nothing

Posted

Thanks for the feedback! as I said before.... I just recently found out

that AutoIt could work with Ternary Operators... Now thank to what

you just showed me... I now know I can "Do a Double"! thanks for

the info... by the way... did you have a look inside my little script I attached?

Where you able to run it ok?

Posted

Yes is ok :)

---------------------------
TernaryOp v1.00 - Condition Detected!
---------------------------
Found that the Condition of $ValueSet01 = TRUE!
---------------------------

 

I know that I know nothing

Posted
20 minutes ago, TreatJ said:

Do a Double

You can do many more, I currently use this in a script...

$SpotX = $P=0?0:$P=1?1:$P=2?2:$P=3?3:$P=4?0:$P=5?1:$P=6?2:$P=7?3:$P=8?0:$P=9?1:$P=10?2:$P=11?3:$P=12?0:$P=13?1:$P=14?2:$P=15?3:0

 

Some guy's script + some other guy's script = my script!

Posted

Wow... and I thought being able to do a "Double" was something!

Thanks for the info.... I can see where that would come in handy...

instead of using a bunch of If/Then statements...

Posted

@Werty good as a tennary example but a bit unreadable as code :)
it need a comment on what it does.


looking up what it does I discovered a more readable approach

$P = 15

$SpotX = $P=0?0:$P=1?1:$P=2?2:$P=3?3:$P=4?0:$P=5?1:$P=6?2:$P=7?3:$P=8?0:$P=9?1:$P=10?2:$P=11?3:$P=12?0:$P=13?1:$P=14?2:$P=15?3:0
ConsoleWrite("$SpotX=" & $SpotX & @CRLF)


$SpotX = Mod($P, 4)
ConsoleWrite("$SpotX=" & $SpotX & @CRLF)

...without being sure though.

I know that I know nothing

Posted (edited)

I should have posted a slightly different example, sometimes I need to change the order so Mod() wouldnt work, but nice example.

Edited by Werty

Some guy's script + some other guy's script = my script!

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...