Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Ternary Operator in Powershell

Reply
 
Old 11-15-2006   #1 (permalink)
klumsy@gmail.com


 
 

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 expression
based 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 SpecsSystem Spec
Old 11-15-2006   #2 (permalink)
klumsy@gmail.com


 
 

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 SpecsSystem Spec
Old 11-15-2006   #3 (permalink)
klumsy@gmail.com


 
 

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 SpecsSystem Spec
Old 11-15-2006   #4 (permalink)
Marty List


 
 

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 SpecsSystem Spec
Old 11-15-2006   #5 (permalink)
Keith Hill [MVP]


 
 

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 SpecsSystem Spec
Old 11-16-2006   #6 (permalink)
Keith Hill [MVP]


 
 

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 SpecsSystem Spec
Old 11-16-2006   #7 (permalink)
Jacques Barathon [MS]


 
 

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 SpecsSystem Spec
Old 11-16-2006   #8 (permalink)
klumsy@gmail.com


 
 

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 SpecsSystem Spec
Old 11-16-2006   #9 (permalink)
Keith Hill [MVP]


 
 

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 SpecsSystem Spec
Old 11-16-2006   #10 (permalink)
Maximilian Hänel


 
 

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 SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
-ne operator PowerShell
Powershell Operator PowerShell
-f operator PowerShell
What does the $() operator do? PowerShell
Ternary conditional operator PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46