删除一个数组中另一个数组中的项目 [英] Delete items in an array that are in another array

查看:56
本文介绍了删除一个数组中另一个数组中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被我的脚本困住了.我需要比较两个数组,一个是查询的结果,另一个是文件的内容:

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 "select name from person"

$Array2 = Get-Content ".\Myfile.txt" #the file is a set of one item
every line

现在,在 $Array2 中有我想从 $Array1 中删除的项目.

Now, in $Array2 there are items that I would like to delete from the $Array1.

我该怎么做?感谢您的帮助!

How can I do this? Thank you for yr help!

推荐答案

使用 Where-Object cmdlet 根据 $Array2 中的内容过滤 $Array1:

Use the Where-Object cmdlet to filter $Array1 based on what is in $Array2:

$Array1 = $Array1 |Where-Object { $Array2 -notcontains $_ }

如果 Invoke-Sqlcmd 返回简单的字符串,这会起作用.如果没有,您可以执行以下操作:

That works if Invoke-Sqlcmd returns simple strings. If not, you could do something like:

$Array1 = $Array1 |Where-Object { $Array2 -notcontains $_.someProperty }

这篇关于删除一个数组中另一个数组中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆