Delete items in an array that are in another array

后端 未结 2 1284
天涯浪人
天涯浪人 2021-01-18 12:16

I\'m stuck with my script. I need to compare two arrays, one is the result of a query, the other one of a content of a file:

$Array1 = Invoke-Sqlcmd -Query          


        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-18 12:52

    If performance is an issue, you can get faster results constructing an alternating regex from one of the arrays, and using it as an array operator against the other.

    $Array1 = Invoke-Sqlcmd -Query "select name from person"
    
    $Array2 = Get-Content ".\Myfile.txt" #the file is a set of one item
    every line
    
    $regex = ‘(?i)^(‘ + (($Array2 |foreach {[regex]::escape($_)}) –join “|”) + ‘)$’
    
    $Array1 -notmatch $regex
    

    Explanation of the code to build the regex here.

提交回复
热议问题