0

Is there a more efficient way to search multiple arrays for a specific value? This isn't that bad but if I had 50 arrays it would get out of hand. I thought about doing a switch too but I was hoping for some kind of array iteration method.

$A = "A","B","C"
$B = "X","Y","Z"
$C = "B"

If ($A -contains $C) {$D = "Contained in Array A"}
ElseIF ($B -contains $C) {$D = "Contained in Array B"}
0

3 Answers 3

2

Here is an alternative to do it a bit more dynamically.

$A = "A","B","C"
$B = "X","Y","Z"
$C = "B"

$ArrayList = (get-variable) | ? {$_.value -is [array]}
Foreach ($Array in $ArrayList)
{
    if ($array.value -contains $c)
    {Write-Output "The array `$$($array.name) contains $C"}
}
Sign up to request clarification or add additional context in comments.

4 Comments

Awesome, I was going to post something like this but didn't think of comparing to the [array] type, and couldn't wrap my head around an alternative method to do that.
I was also struggling to find a way to use get-variable good to know someone got it.
What function do the brackets around "array" serve? -> [array]
It is used to declare a type. Also referred to as "casting" if you are using it to define an object. Another example would be 1 | GM returns type of integer. [string]1 | GM you can see it changes to a string. In this case I am just checking to see if the value property of each object is an array.
0

I can think of other ways but a simple one could be this

$A = "A","B","C"
$B = "X","Y","Z"
$C = "X"

$AllArrays = @{'A'=$A;'B'=$B}
$AllArrays.GetEnumerator() | ForEach-Object{
    If($_.Value -contains $C){
        Write-Host "Found In $($_.Name)"
    }
}

You could build a hash table of all your arrays and then loop through it. Each .Value of the hash table contains one of the arrays. You still use -contains and report back the name of the array from the original hash table. We use the GetEnumerator method which effectively sends each entry in the hash table across the pipeline as a separate object. FYI if your arrays are not statically assigned you could build the hash table with something like $AllArrays.Add('D',$D)

 Found In B

The B comes from 'B'=$B in the hash table. This all depends on how you declare your arrays. There might be cleaner ways depending on how your arrays are populated.

Alternative

Instead of a hashtable you could use a PsCustomObject. Note this code was designed in PowerShell 3.0 but minor changes will make it work in earlier versions. In this snippet I removed the array declarations for brevity.

$allArrays = [PsCustomObject]@{
    'A'=$A
    'B'=$B
}

$allArrays.psobject.Properties | ForEach-Object{
    If($_.Value -contains $C){
        Write-Host "Found In $($_.Name)"
    }   
}

2 Comments

I thought about posting this, but it really ends up being not much less work than creating a Switch to do it, and he was looking for something more general, or shorter than that. Your answer works, I just don't think it was in the spirit of the question.
For 50 some arrays it could be tedious some yes. I would think that one assignment, loop and if statement would be easier to read than a rather large switch statement. @TheMadTechnician I was trying to hint at this being a poor solution only because I doubted that the OP would have a script with 50 some static defined arrays. If that was the case Noahs answer is the best. But i would think that his arrays might come from another source in which case this loop would account for any number of arrays.
0

To answer the question as asked:

$A = "A","B","C"
$B = "X","Y","Z"
$C = "B"

$D= @()

'A','B' |
foreach {
 if ((Get-Variable $_).value -eq $C) 
 { $D += "Contained in array $_"}
}

$D

Contained in array A

Although I suspect there may be better solutions to the problem but can't really speculate without knowing more about it.

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.