0

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"
   }
}
2
  • 1
    Your question is a bit unclear. Do you want to be able to attribute membership to the specific array? Or do you just want to find out if a name exists in any array? Commented Sep 19, 2016 at 19:46
  • Just find if the name exists. For example I would search for "name64" and it's not found, cool. I would search for "name3" and it returned "Name3 found". I can get it for one array, just not sure if there is a better way to search more then one array without doing the same foreach\if statements over and over again for each different array Commented Sep 19, 2016 at 19:52

3 Answers 3

4

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
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that's perfect!!
2

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}

3 Comments

It is still unclear what the OP is looking for in terms of results. Are you assuming he does not want to know which array it came from?... nvm he responded
Perfect, thanks! That's what I was looking for! Just curious but by doing it this way how would you be able to narrow it down to the array which the name was found in?
@Matt Yes, still unclear, I just assumed he was after a boolean answer
0
  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)"
       }
    }
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.