I'm a complete beginner with PS, so please go easy on me. I am trying to write a section of code that removes an entire element from the array if it contains a certain pattern (in this case, 'Apple' and 'Banana').
I wrote the code below as a placeholder for the actual data so I can get a better idea of what's going on. The first iteration removes the proper item, but after that it gets a little confused. Where did I go wrong?
$Text = @("Apple and Orange", "Pear and Grape", "Peach and Mango", "Avocado and Banana")
$x = 0
$NotItem = "Banana"
$NoSales = "Apple"
cls
foreach ($element in $Text)
{
# Remove elemets from array that don't contain purchasable items
if ($element -contains $NotItem -or $element -match $NoSales)
{
$Text[$x] = $null
$Next = $Text[$x]
Write-Host "Condition Met. $element, Step $x, `n`n` Current Array: $Text`n`n` Next Item: $Next`n`n"
}
# If neither applies, move on
else
{
$x = $x + 1
$Next = $Text[$x]
Write-Host "Condition Not Met. $element, Step $x, `n`n` Current Array: $Text`n`n` Next Item: $Next`n`n"
}
}
And here's what the output looks like:
Condition Met. Apple and Orange, Step 0,
Current Array: Pear and Grape Peach and Mango Avocado and Banana
Next Item:
Condition Not Met. Pear and Grape, Step 1,
Current Array: Pear and Grape Peach and Mango Avocado and Banana
Next Item: Pear and Grape
Condition Not Met. Peach and Mango, Step 2,
Current Array: Pear and Grape Peach and Mango Avocado and Banana
Next Item: Peach and Mango
Condition Not Met. Avocado and Banana, Step 3,
Current Array: Pear and Grape Peach and Mango Avocado and Banana
Next Item: Avocado and Banana
I assume you were expecting
'Avocado and Banana'to not be in$Textafter running your code, for this you would also use-matchinstead of-containswhich looks for an exact match in a collection. However, by mutating your$Textarray you would end up with$nullvalues in it which most likely you do not want, it's better to recreate the array in this case:The simplified approach, using
Where-Objectfor filtering would be: