Serijag
Serijag

Reputation: 37

Search through multiple arrays for a name in powershell

So I have a few arrays with names that I want to search though, I would like to keep the arrays separate as they are each specific to a certain group of names. I'm trying to figure out how to search though more then one at the same time. The code I have below is how to search though one array but I'm not sure the best way to search multiple. I tried to add -and $array2 into the foreach but that did not work.

I know I could just add the same block for each array but I'm wondering if there is a cleaner and more efficient way to do that.

$array1 = "name1", "name2", "name3"
$array2 = "name4", "name5", "name6"
$searchname = Read-Host "Enter the name to search for"
foreach($name in $array1){
   if($searchname -eq $name){
       Write-Host "$searchname found"
   }
}

Upvotes: 0

Views: 517

Answers (3)

woxxom
woxxom

Reputation: 73586

  1. Use the PS3+ -in operator: $value -in $array
    or the PS2+ -contains operator: $array -contains $value
  2. In case of big arrays don't concatenate them as it's slow.
  3. Organize the arrays in an array so you can enumerate them easier.

    $arrays = @(
        @("name1", "name2", "name3")
        @("name4", "name5", "name6")
    )
    
    $searchname = Read-Host "Enter the name to search for"
    1..$arrays.count | ForEach {
       if ($searchname -in $arrays[$_-1]) {
           Write-Host "$searchname found in array #$_"
       }
    }
    
  4. Or use a hashtable:

    $arrays = @{
        foo = "name1", "name2", "name3"
        bar = "name4", "name5", "name6"
    }
    
    $searchname = Read-Host "Enter the name to search for"
    ForEach ($entry in $arrays.GetEnumerator()) {
       if ($searchname -in $entry.value) {
           Write-Host "$searchname found in array $($entry.key)"
       }
    }
    

Upvotes: 0

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200293

If you just need to verify whether the name is present in any of the arrays you could simply concatenate them and check if the result contains the name you're looking for:

if (($array1 + $array2) -contains $name) {
    Write-Host "$name found"
}

If you want to identify the array in which it was found you could do something like this:

'array1', 'array2' | ForEach-Object {
    if ((Get-Variable $_).Value -contains $name) {
        Write-Host "$name found in `$$_"
        break
    }
}

or like this, if the arrays were stored in a hashtable rather than individual variables:

$hash = @{
    array1 = "name1", "name2", "name3"
    array2 = "name4", "name5", "name6"
}

$hash.GetEnumerator() | ForEach-Object {
    if ($_.Value -contains $name) {
        Write-Host ('{0} found in ${1}' -f $name, $_.Name)
        break
    }
}

Upvotes: 4

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174485

If you want to search across items in multiple arrays, you can concatenate the arrays in the foreach statement like so:

foreach($name in @($array1;$array2)){
   if($searchname -eq $name){
       Write-Host "$searchname found"
   }
}

A more PowerShell-idiomatic approach would entail using the pipeline with the Where-Object filter cmdlet:

@($array1;$array2) |Where-Object {$_ -eq $searchname}

Upvotes: 2

Related Questions