Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PowerShell顺序敏感比较对象差异_Powershell - Fatal编程技术网

PowerShell顺序敏感比较对象差异

PowerShell顺序敏感比较对象差异,powershell,Powershell,是否可以使用PowerShell中的Compare Objectakadiff对数组中的项进行顺序比较?如果没有,建议一种解决方法 $a1=@(1,2,3,4,5) $b1=@(1,2,3,5,4) if (Compare-Object $a1 $b1) { Write-Host 'Wrong order ' } else { Write-Host 'OK' } 您可以逐个值比较数组中的项,如下所示: $a1=@(1,2,3,4,5,6,7,8,9,10) $b1=@(1,2

是否可以使用PowerShell中的
Compare Object
aka
diff
对数组中的项进行顺序比较?如果没有,建议一种解决方法

$a1=@(1,2,3,4,5)
$b1=@(1,2,3,5,4)
if (Compare-Object $a1 $b1) {
    Write-Host 'Wrong order '
}
else {
    Write-Host 'OK' 
}

您可以逐个值比较数组中的项,如下所示:

$a1=@(1,2,3,4,5,6,7,8,9,10)
$b1=@(1,2,3,5,4,6,7,9,8,10)

$i= 0
$counter = 0
$obj = @()

foreach ($a in $a1)
{
    $sub_obj = New-Object PSObject
    $sub_obj | Add-Member -MemberType NoteProperty -Name Value1 -Value $a      
    $sub_obj | Add-Member -MemberType NoteProperty -Name Value2 -Value $b1[$i]
    if ($a -eq $b1[$i])
    {
         $sub_obj | Add-Member -MemberType NoteProperty -Name IsMatch -Value $true
    }
    else
    {
        $sub_obj | Add-Member -MemberType NoteProperty -Name IsMatch -Value $false
        $counter++
    }
    $obj += $sub_obj
    $i++
}

Write-Output $obj | Format-Table -AutoSize

if ($counter -gt 0)
{Write-Host 'Wrong order!'}
else
{Write-Host 'Correct order!'}
输出:

Value1 Value2 IsMatch
------ ------ -------
     1      1    True
     2      2    True
     3      3    True
     4      5   False
     5      4   False
     6      6    True
     7      7    True
     8      9   False
     9      8   False
    10     10    True


Wrong order!

使用
-SyncWindow 0

$a1=@(1,2,3,4,5)
$b1=@(1,2,3,5,4)
if (Compare-Object $a1 $b1 -SyncWindow 0) {
    Write-Host 'Wrong order '
}
else {
    Write-Host 'OK' 
}

更多信息:。

在本例中,您不能使用if($a1-ne$b1){}吗?如果它们是相同的?你不能简单地对它们进行分类吗
$a1=$a1 | sort object
或通过get-Enumerator()进行排序,如果它们包含不同的内容,但都是按顺序排序的呢?