【问题标题】:Getting Missing element from comparison of Array's using powershell [duplicate]从使用powershell的Array比较中获取缺失元素[重复]
【发布时间】:2011-06-16 18:06:45
【问题描述】:

可能重复:
Comparing two arrays & get the values which are not common

我想要一个从数组中获取不常见项的逻辑,例如:

$a=@(1,2,3,4,5,6)
$b=@(1,2,3,4,5,7,9,10)

我希望 $c 的输出为 6,这是 $b 数组中缺少的元素,应仅优先考虑 $a 的数组内容。

谁能帮我解决这个问题?
谢谢!

【问题讨论】:

  • 查看我对你上一个问题的评论,你会发现(稍加努力)。
  • 嗨@stej 我可以成功!
  • 嗨@stej 这不是一个重复的问题,我希望现在优先考虑数组 $a

标签: arrays powershell


【解决方案1】:

empo 的方法,或者

$a1=@(1,2,3,4,5,8)
$b1=@(1,2,3,4,5,6)
Compare-Object $a1 $b1 | 
   Where-Object { $_.SideIndicator -eq '<=' } | 
   Foreach-Object { $_.InputObject }

返回 8

【讨论】:

    【解决方案2】:
    $c = $a | ? {!($b -contains $_)}
    

    优先级将给予你“管道”的变量。

    【讨论】:

    • 你也可以使用:?{$b -notcontains $_}
    • 或者? { -not ($b -contains $_) },结果是一样的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-07
    • 2021-12-22
    • 2015-11-30
    相关资源
    最近更新 更多