由于 Compare-Object 不能采用通配符,我将如何比较两个数组的内容,这些数组的长度或内容不完全相同,但足够相似,可以让通配符集或正则表达式捕获它们。
例子:
$form = "First Name","Last Name","Address","Phone","Nickname","Fax Number"
$data = "First Name ","Last Name","Street Address","Phone number","",Fax"
我可以在不执行一堆 if 语句和运算符的情况下比较数组吗?开关可能会更优雅,但我只是希望能够在与此类似的一行上执行此操作:
Compare-Object ("*$form*) ("*$data*")
而不是这样:
for($x=0;$x -lt $form.Count;$x++)
{
[string]$xx = $x
if($form[$x] -like "*$data[$x]*" -or $data[$x] -like "*form[$x]*" -and $data[$x] -ne $null -and $form[$x] -ne $null)
{
$form[$x]
$data[$x]
}
elseif($form[$x] -eq $null -and $data[$x] -eq $null)
{
"Form index $xx and Data index $xx are null"
}
elseif($form[$x] -eq $null)
{
"Form index $xx is null"
}
elseif($data[$x] -eq $null)
{
"Data index $xx is null"
}
else
{
"Data index $xx and Form index $xx are not even close to the same"
}
}