![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | Ternary Operator in Powershell One this that has bothered me for some time, is the lack of a ternary operator in powershell (disclaimer, maybe there is one, and i just haven't found it . This seems unintuitive expecially for an expressionbased sort of shell language like powershell.. Well the great thing with powershell, in the way that functions/cmdlets etc are evaluated before keywords , and also particularly scriptblocks, how scriptblocks are first class citizens. So you can easily make a function to do this, so i did. In a couple of lines, and a couple of minutes function Invoke-Ternary ([bool] $decider,[scriptblock]$iftrue,[scriptblock]$iffalse) { if ($decider) { &$iftrue} else { &$iffalse } } set-Alias ?: Invoke-Ternary So you can easily call it like this $a = 10 ?: ($a -gt 5) {"its greater than five"} {"its not greater than 5"} i love powershell. its so easy to adapt. Karl Prosser : http://www.karlprosser.com/coder PowerShell Analyzer : http://www.powershellanalyzer.com |
My System Specs![]() |
| | #2 (permalink) |
| Guest | Re: Ternary Operator in Powershell maybe this technique is even better function Invoke-Ternary ([scriptblock] $decider,[scriptblock]$iftrue,[scriptblock]$iffalse) { begin {} process { if (&$decider) { &$iftrue} else { &$iffalse } } end {} } because it works with the pipeline, and alows the condition to be calculated each time.. i.e in the following examples $a = 10 ?: {$a -gt 5} {"its greater than five"} {"its not greater than 5"} 1..10 | ?: {$_ -gt 5} {"its greater than five";$_} {"its not greater than 5";$_} |
My System Specs![]() |
| | #3 (permalink) |
| Guest | Re: Ternary Operator in Powershell doh, even more simpler filter Invoke-Ternary ([scriptblock] $decider,[scriptblock]$iftrue,[scriptblock]$iffalse) { if (&$decider) { &$iftrue} else { &$iffalse } } |
My System Specs![]() |
| | #4 (permalink) |
| Guest | Re: Ternary Operator in Powershell I like it, definitely a keeper. For others following along, the first version showed this usage example: ?: ($a -gt 5) {"its greater than five"} {"its not greater than 5"} but that won't work with v3, replace () with {}: ?: {$a -gt 5} {"its greater than five"} {"its not greater than 5"} <klumsy@gmail.com> wrote in message news:1163630863.578013.281630@f16g2000cwb.googlegroups.com... > doh, even more simpler > > filter Invoke-Ternary ([scriptblock] > $decider,[scriptblock]$iftrue,[scriptblock]$iffalse) > { > if (&$decider) { &$iftrue} else { &$iffalse } > } > |
My System Specs![]() |
| | #5 (permalink) |
| Guest | Re: Ternary Operator in Powershell <klumsy@gmail.com> wrote in message news:1163630863.578013.281630@f16g2000cwb.googlegroups.com... > doh, even more simpler > > filter Invoke-Ternary ([scriptblock] > $decider,[scriptblock]$iftrue,[scriptblock]$iffalse) > { > if (&$decider) { &$iftrue} else { &$iffalse } > } > BTW, if you would like to see this implemented directly in the language vote on it: https://connect.microsoft.com/feedba...3059&SiteID=99 This one is variant of null coalescing operator in C# for working with null values (and nullables): https://connect.microsoft.com/feedba...3061&SiteID=99 -- Keith |
My System Specs![]() |
| | #6 (permalink) |
| Guest | Re: Ternary Operator in Powershell "Keith Hill [MVP]" <r_keith_hill@mailhot.moc.nospam> wrote in message news:OkiRlsSCHHA.4892@TK2MSFTNGP04.phx.gbl... > This one is variant of null coalescing operator in C# for working with null > values (and nullables): > > https://connect.microsoft.com/feedba...3061&SiteID=99 > Regarding the ?? operator request, the following comes close but doesn't work in a pipeline: # Close but not quite working filter set-alias ?? Invoke-NullAlt filter Invoke-NullAlt ([scriptblock]$expression, [scriptblock]$executeIfNull, [bool]$debug = $false) { if ($debug) {"`$expression is `"$expression`""} if ($expression -ne $null) { $result = &$expression if ($debug) {"`$exprResult is `"$result`""} if ($result -ne $null) { $result } else { &$executeIfNull } } else { &$executeIfNull } } 16# ?? {$logdir} {"${env:systemroot}\System32\LogFiles"} C:\WINDOWS\System32\LogFiles In this case $logdir isn't defined so it evaluates the second scriptblock as the result of the filter. 17# ?? {$env:temp} {"${env:systemroot}\System32\LogFiles"} C:\DOCUME~1\Keith\LOCALS~1\Temp In the case above the first scriptblock evaluates to non-null and that result is used. Here's where I run into problems though: 18# gps | %{?? {$_.Description} {"UNKNOWN"} $true} $expression is "$_.Description" $exprResult is "" UNKNOWN <snip> Is there no way to evaluate the $_ in the $expression scriptblock? -- Keith |
My System Specs![]() |
| | #7 (permalink) |
| Guest | Re: Ternary Operator in Powershell You declared it as a filter, so you can use it directly without having to parse the pipeline input with foreach: PS> gps | ?? {$_.description} {"UNKNOWN"} UNKNOWN UNKNOWN UNKNOWN UNKNOWN UNKNOWN Desktop Window Manager Windows Explorer UNKNOWN Microsoft Firewall Client Management .... Jacques "Keith Hill [MVP]" <r_keith_hill@mailhot.moc.nospam> wrote in message news:O004A4TCHHA.4024@TK2MSFTNGP04.phx.gbl... "Keith Hill [MVP]" <r_keith_hill@mailhot.moc.nospam> wrote in message news:OkiRlsSCHHA.4892@TK2MSFTNGP04.phx.gbl... > This one is variant of null coalescing operator in C# for working with null > values (and nullables): > > https://connect.microsoft.com/feedba...3061&SiteID=99 > Regarding the ?? operator request, the following comes close but doesn't work in a pipeline: # Close but not quite working filter set-alias ?? Invoke-NullAlt filter Invoke-NullAlt ([scriptblock]$expression, [scriptblock]$executeIfNull, [bool]$debug = $false) { if ($debug) {"`$expression is `"$expression`""} if ($expression -ne $null) { $result = &$expression if ($debug) {"`$exprResult is `"$result`""} if ($result -ne $null) { $result } else { &$executeIfNull } } else { &$executeIfNull } } 16# ?? {$logdir} {"${env:systemroot}\System32\LogFiles"} C:\WINDOWS\System32\LogFiles In this case $logdir isn't defined so it evaluates the second scriptblock as the result of the filter. 17# ?? {$env:temp} {"${env:systemroot}\System32\LogFiles"} C:\DOCUME~1\Keith\LOCALS~1\Temp In the case above the first scriptblock evaluates to non-null and that result is used. Here's where I run into problems though: 18# gps | %{?? {$_.Description} {"UNKNOWN"} $true} $expression is "$_.Description" $exprResult is "" UNKNOWN <snip> Is there no way to evaluate the $_ in the $expression scriptblock? -- Keith |
My System Specs![]() |
| | #8 (permalink) |
| Guest | Re: Ternary Operator in Powershell sure there is , you don't need the foreach as this is a filter, and you are processing it via the pipeline so instead of gps | %{?? {$_.Description} {"UNKNOWN"} $true} try gps | ?? {$_.Description} {"UNKNOWN"} $true |
My System Specs![]() |
| | #9 (permalink) |
| Guest | Re: Ternary Operator in Powershell <klumsy@gmail.com> wrote in message news:1163698564.620489.240720@m7g2000cwm.googlegroups.com... > sure there is , you don't need the foreach as this is a filter, and you > are processing it via the pipeline > > so instead of > > gps | %{?? {$_.Description} {"UNKNOWN"} $true} > > try > > gps | ?? {$_.Description} {"UNKNOWN"} $true Doh! :-) Thanks for pointing that out. -- Keith |
My System Specs![]() |
| | #10 (permalink) |
| Guest | Re: Ternary Operator in Powershell Hi klumsy, > So you can easily call it like this > > $a = 10 > ?: ($a -gt 5) {"its greater than five"} {"its not greater than 5"} > > i love powershell. its so easy to adapt. IMHO it would be much nicer to have a built-in ternary operator so one could write this: $a=$true $b=$a?"True":"False" or $a = 10 $b=$a -gt 5?"its greater than five":"its not greater than 5" or $b=$a -gt 5?5:$a cu Max |
My System Specs![]() |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Powershell Operator | AdityaKir | PowerShell | 3 | 08-18-2008 04:55 PM |
| -f operator | Tibor Soos | PowerShell | 3 | 05-28-2008 07:36 AM |
| What does the $() operator do? | Kevin Buchan | PowerShell | 3 | 02-08-2008 02:05 PM |
| Ternary conditional operator | Duncan Smith | PowerShell | 2 | 09-18-2007 10:29 AM |
| Suggestion: 1 new operator (well, maybe 6...) | dreeschkind | PowerShell | 10 | 07-12-2007 11:34 AM |