Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell equivalent to Python "in"?

I've been using arrays lately and really missing Python's "in" operator.

e.g.:

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

I've made up for it a little bit by creating a "ThereExists-Object" function, like so:

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

e.g.:

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

obviously I could define another function for this as well... but I'd like to know if there's some syntactical sugar that I'm unfamiliar with that could get this job done.

So... is there any?

like image 352
Nacht Avatar asked Apr 11 '12 01:04

Nacht


People also ask

Can PowerShell be used for Python?

PowerShell is a commandline user interface for Windows that is often used as part of a Python programmer's development environment. PowerShell is an implementation of the shells concept. Learn more in the development environments chapter or view the table of contents for all topics.

How similar is PowerShell to Python?

Python is an interpreted high-level programming language whereas PowerShell provides a shell scripting environment for Windows and is a better fit if you choose to automate tasks on the Windows platform.

What does @() mean in PowerShell?

Array subexpression operator @( )Returns the result of one or more statements as an array. The result is always an array of 0 or more objects. PowerShell Copy.

How do I run a Python script in PowerShell?

With your PowerShell command line open, enter python to run the Python 3 interpreter. (Some instructions prefer to use the command py or python3 , these should also work). You will know that you're successful because a >>> prompt with three greater-than symbols will display.


1 Answers

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

source: http://technet.microsoft.com/en-us/library/ee692798.aspx

like image 54
Craig O Avatar answered Oct 12 '22 10:10

Craig O