{"id":8961,"date":"2006-12-29T15:30:00","date_gmt":"2006-12-29T15:30:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2006\/12\/29\/diy-ternary-operator\/"},"modified":"2019-02-18T13:20:58","modified_gmt":"2019-02-18T20:20:58","slug":"diy-ternary-operator","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/diy-ternary-operator\/","title":{"rendered":"DIY: Ternary operator"},"content":{"rendered":"<p><!--\n \/* Font Definitions *\/\n @font-face\n\t{font-family:\"Cambria Math\";\n\tpanose-1:2 4 5 3 5 4 6 3 2 4;}\n@font-face\n\t{font-family:Cambria;\n\tpanose-1:2 4 5 3 5 4 6 3 2 4;}\n@font-face\n\t{font-family:Calibri;\n\tpanose-1:2 15 5 2 2 2 4 3 2 4;}\n@font-face\n\t{font-family:\"Lucida Console\";\n\tpanose-1:2 11 6 9 4 5 4 2 2 4;}\n \/* Style Definitions *\/\n p.MsoNormal, li.MsoNormal, div.MsoNormal\n\t{margin-top:0in;\n\tmargin-right:0in;\n\tmargin-bottom:10.0pt;\n\tmargin-left:.75in;\n\tline-height:115%;\n\tfont-size:11.0pt;\n\tfont-family:\"Calibri\",\"sans-serif\";}\nh2\n\t{mso-style-link:\"Heading 2 Char\";\n\tmargin-top:10.0pt;\n\tmargin-right:0in;\n\tmargin-bottom:0in;\n\tmargin-left:.75in;\n\tmargin-bottom:.0001pt;\n\tline-height:115%;\n\tpage-break-after:avoid;\n\tfont-size:13.0pt;\n\tfont-family:\"Cambria\",\"serif\";\n\tcolor:#4F81BD;\n\tfont-weight:bold;}\na:link, span.MsoHyperlink\n\t{color:blue;\n\ttext-decoration:underline;}\na:visited, span.MsoHyperlinkFollowed\n\t{color:purple;\n\ttext-decoration:underline;}\np\n\t{margin-right:0in;\n\tmargin-left:0in;\n\tfont-size:12.0pt;\n\tfont-family:\"Times New Roman\",\"serif\";}\np.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph\n\t{margin-top:0in;\n\tmargin-right:0in;\n\tmargin-bottom:10.0pt;\n\tmargin-left:.5in;\n\tline-height:115%;\n\tfont-size:11.0pt;\n\tfont-family:\"Calibri\",\"sans-serif\";}\nspan.Heading2Char\n\t{mso-style-name:\"Heading 2 Char\";\n\tmso-style-link:\"Heading 2\";\n\tfont-family:\"Cambria\",\"serif\";\n\tcolor:#4F81BD;\n\tfont-weight:bold;}\np.msolistparagraphcxspfirst, li.msolistparagraphcxspfirst, div.msolistparagraphcxspfirst\n\t{mso-style-name:msolistparagraphcxspfirst;\n\tmargin-top:0in;\n\tmargin-right:0in;\n\tmargin-bottom:0in;\n\tmargin-left:.5in;\n\tmargin-bottom:.0001pt;\n\tline-height:115%;\n\tfont-size:11.0pt;\n\tfont-family:\"Calibri\",\"sans-serif\";}\np.msolistparagraphcxspmiddle, li.msolistparagraphcxspmiddle, div.msolistparagraphcxspmiddle\n\t{mso-style-name:msolistparagraphcxspmiddle;\n\tmargin-top:0in;\n\tmargin-right:0in;\n\tmargin-bottom:0in;\n\tmargin-left:.5in;\n\tmargin-bottom:.0001pt;\n\tline-height:115%;\n\tfont-size:11.0pt;\n\tfont-family:\"Calibri\",\"sans-serif\";}\np.msolistparagraphcxsplast, li.msolistparagraphcxsplast, div.msolistparagraphcxsplast\n\t{mso-style-name:msolistparagraphcxsplast;\n\tmargin-top:0in;\n\tmargin-right:0in;\n\tmargin-bottom:10.0pt;\n\tmargin-left:.5in;\n\tline-height:115%;\n\tfont-size:11.0pt;\n\tfont-family:\"Calibri\",\"sans-serif\";}\nspan.CodeChar\n\t{mso-style-name:\"Code Char\";\n\tmso-style-link:Code;\n\tfont-family:\"Lucida Console\";\n\tcolor:#EEEDF0;\n\tbackground:#012456;}\np.Code, li.Code, div.Code\n\t{mso-style-name:Code;\n\tmso-style-link:\"Code Char\";\n\tmargin-top:0in;\n\tmargin-right:0in;\n\tmargin-bottom:12.0pt;\n\tmargin-left:.75in;\n\tline-height:115%;\n\tbackground:#012456;\n\tfont-size:8.0pt;\n\tfont-family:\"Lucida Console\";\n\tcolor:#EEEDF0;}\np.msopapdefault, li.msopapdefault, div.msopapdefault\n\t{mso-style-name:msopapdefault;\n\tmargin-top:0in;\n\tmargin-right:0in;\n\tmargin-bottom:10.0pt;\n\tmargin-left:.75in;\n\tline-height:115%;\n\tfont-size:12.0pt;\n\tfont-family:\"Times New Roman\",\"serif\";}\n.MsoChpDefault\n\t{font-size:10.0pt;}\n@page Section1\n\t{size:8.5in 11.0in;\n\tmargin:1.0in 1.0in 1.0in 1.0in;}\ndiv.Section1\n\t{page:Section1;}\n--><\/p>\n<p>&lt;Changed DYI&nbsp;=&gt; DIY.&nbsp;&nbsp;jps&gt; &nbsp;<\/p>\n<p>At Microsoft, &#8220;to ship is to choose&#8221;.&nbsp; One of the things we were very disappointed in not being able to ship in V1.0 is a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Ternary_operation\">ternary operator<\/a>. Here is a description of a ternary operator from Wikipedia:<\/p>\n<blockquote>\n<p>Many programming languages that use <a title=\"C syntax\" href=\"http:\/\/en.wikipedia.org\/wiki\/C_syntax\">C-like syntax<\/a> feature a ternary operator, <code><a title=\"?:\" href=\"http:\/\/en.wikipedia.org\/wiki\/%3F:\">?:<\/a><\/code>; unqualified, &#8220;ternary operator&#8221; usually refers to this. The <code>?:<\/code> operator is used as a shorthand replacement for the if-then-else <a title=\"Conditional statement\" href=\"http:\/\/en.wikipedia.org\/wiki\/Conditional_statement\">conditional statement<\/a>; the general form is <i>condition&nbsp;? op1&nbsp;: op2<\/i>. If <i>condition<\/i> is true, the statement evaluates as <i>op1<\/i>; otherwise, it evaluates as <i>op2<\/i>.<\/p>\n<\/blockquote>\n<p>Imagine the scenario where you give a discount of 10%&nbsp;all orders but 25% off those with a quanity greater than 10.&nbsp; Without a ternary operator, you code it this way:<\/p>\n<p class=\"Code\">if ($quantity -le 10)<br \/>{&nbsp;&nbsp; $total = ($quantity * $price) * .9<br \/>}else<br \/>{&nbsp;&nbsp; $total = ($quantity * $price) * .75<br \/>}<\/p>\n<p><strong>If<\/strong> (when?) PowerShell supports&nbsp;ternary operators you will be able to do it this way:<\/p>\n<p class=\"Code\">$total = ($quantity * $price ) * ({$quantity -le 10} ? {.9} : {.75})<\/p>\n<p>This would evalue the expression {$quantity -le 10} and if it is TRUE, it evaluates the first script block and uses that value {.9}, otherwise, it evaluates the second scriptblock and uses that value {.75}.<\/p>\n<p>Did I mention that we didn&#8217;t get around to supporting this in V1.0?&nbsp; Well, the community comes to the rescue.&nbsp; <\/p>\n<p>In the <a href=\"http:\/\/www.codeplex.com\/Wiki\/View.aspx?ProjectName=PowerShellCX\">PowerShell Community Extensions<\/a> project over on CodePlex.com (which you can download\/install from&nbsp;&nbsp;<a href=\"http:\/\/www.codeplex.com\/Wiki\/View.aspx?ProjectName=PowerShellCX\">http:\/\/www.codeplex.com\/Wiki\/View.aspx?ProjectName=PowerShellCX<\/a> ), they have a ton of great stuff.&nbsp; I installed this a while ago and am now getting around to investigating what is there (silly me &#8211; I should have done this on day 1).&nbsp; <a href=\"http:\/\/www.karlprosser.com\/coder\/?cat=2\">Karl Prosser<\/a> contributed a script called Invoke-Ternary and then defined an alias ?:&nbsp; .&nbsp; Using this does not provide the same syntax that you would get if\/when the PowerShell parser supports this but it is pretty great.&nbsp; <\/p>\n<p>Here is the code:<\/p>\n<p class=\"Code\"># &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/># Name:&nbsp;&nbsp; Invoke-Ternary<br \/># Alias:&nbsp; ?:<br \/># Author: Karl Prosser<br \/># Desc:&nbsp;&nbsp; Similar to the C# ? : operator e.g. <br \/>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _name = (value != null) ? String.Empty : value;<br \/># Usage:&nbsp; 1..10 | ?: {$_ -gt 5} {&#8220;Greater than 5;$_} {&#8220;Not greater than 5&#8221;;$_}<br \/># &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br \/>set-alias ?: Invoke-Ternary -Option AllScope -Description &#8220;PSCX filter alias&#8221;<br \/>filter Invoke-Ternary ([scriptblock]$decider, [scriptblock]$ifTrue, [scriptblock]$ifFalse) <br \/>{<br \/>&nbsp;&nbsp; if (&amp;$decider) {&nbsp;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &amp;$ifTrue<br \/>&nbsp;&nbsp; }&nbsp;else {&nbsp;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &amp;$ifFalse&nbsp;<br \/>&nbsp;&nbsp;&nbsp;}<br \/>}<\/p>\n<p>This is a very tight, concise piece of code which is worthy of study.&nbsp; What Karl is doing here is to leverage PowerShell&#8217;s ability to treat code as a first class datatype [SCRIPTBLOCK] to<strong><u> CREATE HIS OWN CONTROL STRUCTURE!<\/u><\/strong>&nbsp; He accepts 3 scriptblocks, he evaluates the first and if it is TRUE, he evaluates and returns the value of the second, otherwise it evalutes and returns the value of the third.&nbsp; So going back to our original example, you can now code it this way (Notice the Syntax):<\/p>\n<p class=\"Code\">$total = ($quantity * $price ) * (?:&nbsp; {$quantity -le 10}&nbsp;{.9} {.75})<\/p>\n<p>Enjoy!<\/p>\n<p>Jeffrey Snover [MSFT]<br \/>Windows PowerShell\/MMC Architect<br \/>Visit the Windows PowerShell Team blog at:&nbsp;&nbsp;&nbsp; <a href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/a><br \/>Visit the Windows PowerShell ScriptCenter at:&nbsp; <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&lt;Changed DYI&nbsp;=&gt; DIY.&nbsp;&nbsp;jps&gt; &nbsp; At Microsoft, &#8220;to ship is to choose&#8221;.&nbsp; One of the things we were very disappointed in not being able to ship in V1.0 is a ternary operator. Here is a description of a ternary operator from Wikipedia: Many programming languages that use C-like syntax feature a ternary operator, ?:; unqualified, &#8220;ternary [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[72],"class_list":["post-8961","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-languagecontrol"],"acf":[],"blog_post_summary":"<p>&lt;Changed DYI&nbsp;=&gt; DIY.&nbsp;&nbsp;jps&gt; &nbsp; At Microsoft, &#8220;to ship is to choose&#8221;.&nbsp; One of the things we were very disappointed in not being able to ship in V1.0 is a ternary operator. Here is a description of a ternary operator from Wikipedia: Many programming languages that use C-like syntax feature a ternary operator, ?:; unqualified, &#8220;ternary [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/8961","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=8961"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/8961\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=8961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=8961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=8961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}