2

I have a simple program here that will write out a line within an array of lines if it does not contain any of the names within another array.

[array]$names = "Adam", "Bill", "Colin", "Dave"

[array]$lines = "My name is Jim", "My name is Sam", "My name is Adam"

foreach ($line in $lines)
{
   if ($line -notmatch $names)
   {
       write $line
   }
}

When I run this, it just writes out every line from the array of lines, even though 'Adam' is included in the array of names. It should just write out 'My name is Jim' and 'My name is Sam'.

Sorry if this question is pretty basic but I couldn't find any answers for it.

3 Answers 3

3

If you are using a regex comparison with -notmatch why not turn your list of names into a better regex? How about something like this:

$names = "Adam", "Bill", "Colin", "Dave"
$lines = "My name is Jim", "My name is Sam", "My name is Adam"

$regex = $names -join '|'

$lines | ? {$_ -notmatch $regex} | % {Write-Host $_}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I never thought of joining the strings together.
1

There is probably a better solution than this, but one way to solve it is to have a second loop that iterates through each name and checks for them in the line:

[array]$names = "Adam", "Bill", "Colin", "Dave"
[array]$lines = "My name is Jim", "My name is Sam", "My name is Adam"

foreach ($line in $lines)
{
    $names | ForEach-Object -Begin {$found = $false} {
        If ($line -match $_){ $found = $true; break } 
    } 

    if (-not $found)
    {
        write $line
    }
}

Explanation:

  • Sends $names via the pipeline in to ForEach-Object. This starts by initializing a $found variable within it's -Begin block.
  • ForEach-Object checks the line to match against each name (the name is now an item in the pipeline represented by $_). If it is found it sets $found to true and uses break to end the loop.
  • If $found is not true, it writes the line.

1 Comment

Thanks for this. Works perfect! You would think there would be simpler solutions for such a seemingly easy problem!
0

Thanks for the answers guys. I have reworked my code to make it a little simpler:

[array]$names = "Adam", "Bill", "Colin", "Dave"

[array]$lines = "My name is Jim", "My name is Sam", "My name is Adam"

    foreach ($line in $lines)
    {
        $regex = $names -join '|'

        if ($line -notmatch $regex)
        {
            write $line
        }
    }

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.