5

私は最近配列を使用しており、Python の「in」演算子が本当に欠けています。

例えば:

if ("hello" in ["hello", "there", "sup"]):
    print "this prints :)"

次のように、「ThereExists-Object」関数を作成することで、少し補いました。

function ThereExists-Object([System.Management.Automation.ScriptBlock] $sb)
{
    return ($input | where $sb) -as [bool]
}
New-Alias -Name ThereExists -Value ThereExists-Object

例えば:

if ($arrayOfStuff | thereexists { $_ -eq "hello" } )
{
    write-host "this prints too"
}

明らかに、これに対して別の関数を定義することもできます...しかし、この仕事を成し遂げることができる、私がよく知らない構文糖衣があるかどうか知りたいです。

それで... ありますか?

4

2 に答える 2

9
$arrColors = "blue", "red", "green", "yellow", "white", "pink", "orange", "turquoise"
$arrColors -contains "black" 
False
$arrColors -contains "blue"
True

ソース: http://technet.microsoft.com/en-us/library/ee692798.aspx

于 2012-04-11T01:32:20.220 に答える
3

-inPowershell v3.0以降では、演算子自体を使用できます。

PS> "hello" -in "hello", "there", "sup"
True
PS> "hell" -in "hello", "there", "sup"
False
于 2012-04-11T03:46:49.587 に答える