【问题标题】:Remove matches when comparing arrays比较数组时删除匹配项
【发布时间】:2016-05-12 14:51:25
【问题描述】:

我想帮助比较两个数组,比如用户,并丢弃两个数组中存在或匹配的任何用户,然后将结果扔到最终数组中。例如:

###define arrays
$array1 = @("bill","eric","james","sarah")
$array2 = @("bill","scott","sarah","nancy")

###Combine/Filter? arrays and remove users that exist in both arrays
$result = ($array1 + $array2 | some fancy match removal goes here)
$result
eric,james,scott,nancy

我想确保在合并时从两个数组中完全删除匹配项。因此,如果两个数组中都存在“sarah”,我想将她从最终结果中完全删除。这可能吗?

【问题讨论】:

    标签: arrays powershell compare powershell-4.0


    【解决方案1】:

    使用Compare-Object 提取两个源数组中唯一的元素:

    $result = Compare-Object $array1 $array2 | Select-Object -Expand InputObject
    

    【讨论】:

    • 这太棒了 - 非常感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-08
    • 2021-03-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-13
    相关资源
    最近更新 更多