Jump to content

Ternary Operator Syntax - throwing errors


Recommended Posts

Can someone please explain why this expression keeps throwing errors? I've tried so many different combinations of parentheses around each part/the entire expression. 

For $row = 0 to UBound($aTableData,1)-1

    (Mod($row,7)=0) ? (Null) : (ContinueLoop)

This gives "Error: Missing right bracket ')' in expression"

Sometimes with other parentheses combinations I just get 'Error in expression' which I'm assuming is just a syntax error but at this point I'm not sure.

The $aTableData is an array of 100+ rows and the oddest part is that when $row = 0, the expression evaluates as expected but when it moves on, I keep getting these errors. It works fine with a plain If-Then statement so I'm assuming this has to be syntax.

Link to comment
Share on other sites

Why not use this one liner...

If Mod($row, 7) <> 0 Then ContinueLoop

... instead? Using ternary operators is strange when you don't want to return something from them.

Edit: Was ninja-d.

Edited by seadoggie01

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

The issue is that the result isn't being used... you're returning Null or ContinueLoop... to nothing. The example shows the result being posted to the message box, where it's used in that code.

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

Ah so on the first iteration, returning Null works as I would hope because that causes nothing to happen. However on $row=1, it returns a ContinueLoop but does not actually execute it and this somehow causes an error. I get it a little more now, but still a little hazy on why I get that specific error message. Although I guess either way, I'm using it in the wrong context.

Link to comment
Share on other sites

Are you using SciTE? I can't even get the code to execute once, AU3Check throws a bunch of syntax errors. I highly suggest installing it, it's great for finding errors before you start :)

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

I think if you use a variable to store the result of the ternary operation it should work  ... just preface your ternary with something to capture it and it should execute the function.  Some examples below.

$number = 5

$result=(Mod($number,2)<>0) ? (MsgBox(0,'',"odd")) : (MsgBox(0,'',"even"))


$functionResult=(Mod($number,2)<>0) ? _helloTernary() : (MsgBox(0,'',"even"))

func _helloTernary()
    for $a=0 to 9
        ConsoleWrite("This function is working!  You selected an odd number"&@CRLF)
    Next
EndFunc

 

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Link to comment
Share on other sites

Isnt part of the issue that ContinueLoop is not a thing that returns a thing?

There are some old lengthy threads by @guinness where he gets religious about ternary not being a replacement for If/Then.  I think this sort of example is part of that.

Edited by iamtheky
i am confusing af

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

@Jfish Even that doesn't work with ContinueLoop, I tried just to be sure. You'll get an error about a missing/mismatched parenthesis --> (

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

This does seem like a good example why its not just a replacement for any if-then. And imo creating a new variable sort of defeats the purpose of being a shorthand as you're doing something extra to make it work. I'm too lazy for all that. My takeaway, just use ternarys for returning values and not executing things.

Link to comment
Share on other sites

The ternary operator is special.  One might think it evaluates to a RHS, whatever dataype it is.
But it's not that simple, maybe because it has been introduced lately in the parser.

One would think the two snippets below are equivalent (and both incorrect), but only the second one correctly raises a runtime error:

For $i = 0 To 3
    $i = 2 ? 42 : 42
Next

For $i = 0 To 3
    42
Next

Also ContinueLoop is a statement (like Exit, Wend, EndIf, Select, ...) so it's illegal as a RHS value.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

  • 3 weeks later...
On 8/29/2019 at 8:01 PM, iamtheky said:

Isnt part of the issue that ContinueLoop is not a thing that returns a thing?

There are some old lengthy threads by @guinness where he gets religious about ternary not being a replacement for If/Then.  I think this sort of example is part of that.

Yep, my opinion hasn't changed on that one!

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Ternary operator is for evaluating expressions.  ContinueLoop or ExitLoop are statements.

It's like you try to pass ContinueLoop  to a function  as parameter ( Cos(ExitLoop)) or you try to operate with a statement  like ( $var=1+ContinueLoop)

Saludos

Edited by Danyfirex
Link to comment
Share on other sites

You can use ternary to execute two different "all-in-one-commands", depending on a true/false value.

 

I use that for setting Opt() values depending on if the script is compiled or not.

Just "trow away" the returned result value by assigning it to a not used variable:

$trash=@Compiled?(opt("trayautopause",0)):(opt("Trayicondebug",1))
; MsgBox(0,@Compiled,"check tray icon now")

 

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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