2010-03-05 16 views
6

पावरशेल स्क्रिप्ट पर काम करना मेरे पास कई जगहें थीं जहां मैं चाहता था कि जब तक यह शून्य न हो, अन्यथा बी। अनिवार्य रूप से ?? ऑपरेटर सी # में। मैंने नीचे दिखाए गए फ़ंक्शन को लिखना समाप्त कर दिया, लेकिन मैं मदद नहीं कर सकता लेकिन सोचता हूं कि ऐसा करने का एक अंतर्निहित तरीका है।शून्य मानों के लिए डिफ़ॉल्ट

क्या कोई बेहतर, अंतर्निहित, तरीका है?

function Get-ValueOrDefault() 
{ 
    foreach ($value in $args) 
    { 
     if ($value -ne $null) { return $value } 
    } 
} 

मैं इस काम करता है बेहतर लगता है:

function Get-ValueOrDefault() { $args | select -first 1 } 
+1

एक समस्या यह है कि आप होगा यदि आप बूलियन्स पारित है अप्रत्याशित परिणाम इसके बजाय $ value -eq $ null की तुलना करनी चाहिए। – Josh

+0

अच्छा बिंदु, जोश। – OldFart

+0

इसी तरह के प्रश्न में http://stackoverflow.com/questions/10623907/null-coalescing-in-powershell का सुझाव दिया गया था ($ मान, 'डिफ़ॉल्ट' -एक $ शून्य) [0] उत्तर http: // stackoverflow में। कॉम/ए/17647824/52277 –

उत्तर

5

इसी को हम में प्रदान करना है PowerShell Community Extensions:

अपने कार्य के साथ
<# 
.SYNOPSIS 
    Similar to the C# ?? operator e.g. name = value ?? String.Empty 
.DESCRIPTION 
    Similar to the C# ?? operator e.g. name = value ?? String.Empty; 
    where value would be a Nullable&lt;T&gt; in C#. Even though PowerShell 
    doesn't support nullables yet we can approximate this behavior. 
    In the example below, $LogDir will be assigned the value of $env:LogDir 
    if it exists and it's not null, otherwise it get's assigned the 
    result of the second script block (C:\Windows\System32\LogFiles). 
    This behavior is also analogous to Korn shell assignments of this form: 
    LogDir = ${$LogDir:-$WinDir/System32/LogFiles} 
.PARAMETER PrimaryExpr 
    The condition that determines whether the TrueBlock scriptblock is used or the FalseBlock 
    is used. 
.PARAMETER AlternateExpr 
    This block gets evaluated and its contents are returned from the function if the Conditon 
    scriptblock evaluates to $true. 
.EXAMPLE 
    C:\PS> $LogDir = ?? {$env:LogDir} {"$env:windir\System32\LogFiles"} 
    $LogDir is set to the value of $env:LogDir unless it doesn't exist, in which case it 
    will then default to "$env:windir\System32\LogFiles". 
#> 
filter Invoke-NullCoalescing { 
    param([scriptblock]$PrimaryExpr = $(throw "Parameter '-primaryExpr' (position 1) required"), 
      [scriptblock]$AlternateExpr = $(throw "Parameter '-alternateExpr' (position 2) required")) 

    if ($primaryExpr -ne $null) { 
     $result = &$primaryExpr 
     if ($result -ne $null -and "$result" -ne '') { 
      $result 
     } 
     else { 
      &$alternateExpr 
     } 
    } 
    else { 
     &$alternateExpr 
    } 
} 

New-Alias ?? Invoke-NullCoalescing 

PS> ?? {$xyzzy} {"empty"} 
empty 

PS> ?? {$psversiontable} {"empty"} 

Name       Value 
----       ----- 
CLRVersion      2.0.50727.4927 
BuildVersion     6.1.7600.16385 
PSVersion      2.0 
WSManStackVersion    2.0 
PSCompatibleVersions   {1.0, 2.0} 
SerializationVersion   1.1.0.1 
PSRemotingProtocolVersion  2.1 
+0

क्या ऐसा कुछ है जो पीएससीएक्स प्रदान नहीं करता है? ;) – stej

+0

ओह हाँ, नई सामग्री के लिए जगह है। :-) –

+0

मैं इस उत्तर को स्वीकार नहीं कर रहा हूं क्योंकि मैं ऐसा कुछ ढूंढ रहा था जिसके लिए किसी भी नए कार्यों को परिभाषित करने की आवश्यकता नहीं है, दृष्टि से प्रसन्नता हो रही है, और जिसका उद्देश्य स्क्रिप्ट पढ़ने वाले किसी के लिए काफी स्पष्ट है। – OldFart

संबंधित मुद्दे