0

以下のコードスニピットがあります

$dllCheckList = import-csv "c:\path\dllCheckList.csv" 
#dllCheckList.csv contains two items 'test1.dll' , 'test2.dll'

$resultsArray1 = @("test2.dll","test3.dll")
$resultsArray2 = @("test3.dll","test4.dll")
$resultsArray3 = @("test1.dll","test2.dll")

$resultsArray1writes test2.dll$resultsArray2何も書き込まない、および$resultsArray3writesを行うために、どのような種類の比較/ループを使用しますかtest.dll, test2.dll

4

3 に答える 3

3

使用することをお勧めしますCompare-Object <array1> <array2> -passthru -excludeDifferent -includeEqual

于 2013-10-31T22:00:41.253 に答える
0

これを行う 1 つの方法は、csv データからオンザフライで構築された正規表現を使用し、それを各配列に対して -match と共に使用することです。これにより、csv 内のすべてのエントリを通じてすべての配列を反復処理する必要がなくなります。csv がどのように見えるかはわかりませんが、それをインポートしてから、最初にすべての dll 名の配列を構築するか、配列の代わりに csv を直接使用するようにこれを変更する必要があります。

#$dllCheckList = import-csv "c:\path\dllCheckList.csv" 
#dllCheckList.csv contains two items 'test1.dll' , 'test2.dll'

$dll_checklist = @('test1.dll','test2.dll')


$resultsArray1 = @("test2.dll","test3.dll")
$resultsArray2 = @("test3.dll","test4.dll")
$resultsArray3 = @("test1.dll","test2.dll")

[regex] $dll_regex = ‘(?i)^(‘ + (($dll_checklist |foreach {[regex]::escape($_)}) –join “|”) + ‘)$’

$resultsArray1,$resultsArray2,$resultsArray3 |
foreach {$_ -match $dll_regex}

test2.dll
test1.dll
test2.dll

正規表現を作成している行の説明は、http: //blogs.technet.com/b/heyscriptingguy/archive/2011/02/18/speed-up-array-comparisons-in-powershell-with-にあります。 a-runtime-regex.aspx

于 2013-10-31T22:25:18.590 に答える
0

の出力$resultsArray3は、ヘッダー付きの実際の CSVtest1.dll, test2.dllではなく、 and演算子を使用するとtest.dll, test2.dll仮定すると、次のようになります。c:\path\dllCheckList.csv-contains-join

PS C:\> $resultsArray1 = @("test2.dll","test3.dll")
PS C:\> $resultsArray2 = @("test3.dll","test4.dll")
PS C:\> $resultsArray3 = @("test1.dll","test2.dll")
PS C:\> cat 'C:\path\dllCheckList.csv'
Libs
test1.dll
test2.dll
PS C:\> $dllCheckList = Import-Csv 'C:\path\dllCheckList.csv' | % { $_.Libs }
PS C:\> $dllCheckList
test1.dll
test2.dll
PS C:\> ($resultsArray1 | ? { $dllCheckList -contains $_ }) -join ', '
test2.dll
PS C:\> ($resultsArray2 | ? { $dllCheckList -contains $_ }) -join ', '

PS C:\> ($resultsArray3 | ? { $dllCheckList -contains $_ }) -join ', '
test1.dll, test2.dll
于 2013-11-01T00:13:12.557 に答える