Powershell ArrayList孤立对象
我遇到了一个奇怪的问题,我使用多个for循环,并使用基于循环作为索引的对象属性构建的字符串填充arraylist 我发现,如果我Powershell ArrayList孤立对象,powershell,arraylist,Powershell,Arraylist,我遇到了一个奇怪的问题,我使用多个for循环,并使用基于循环作为索引的对象属性构建的字符串填充arraylist 我发现,如果我写入主机将所有行添加到arraylist中。屏幕上打印的内容正是我希望在arraylist中看到的内容,但是当我使用以下内容循环遍历数组列表时: foreach ($r in $Result) { Write-Host -ForegroundColor Yellow "Object: $r" } 我在arraylist中看到了大量我没有预料到的条目。我不知道为
写入主机
将所有行添加到arraylist中。屏幕上打印的内容正是我希望在arraylist中看到的内容,但是当我使用以下内容循环遍历数组列表时:
foreach ($r in $Result) {
Write-Host -ForegroundColor Yellow "Object: $r"
}
我在arraylist中看到了大量我没有预料到的条目。我不知道为什么。我已在此处发布了该功能:
function Process-Config () {
param(
[string]$XMLPath
)
# Validate Parameters
if (Test-Path $XMLPath) { } else { exit }
Add-Type -path C:\Users\user\Downloads\ChilkatDotNet2-9.5.0-x64\ChilkatDotNet2-9.5.0-x64\ChilkatDotNet2.dll
$xml1 = New-Object Chilkat.Xml
$success = $xml1.LoadXmlFile($XMLPath)
if ($success -ne $true) {
$($xml.LastErrorText)
exit
}
$("NumChildren = " + $xml1.NumChildren)
Write-Host -ForegroundColor yellow "here"
# Array list that holds each string so it can be compared later on
$ResponseStrings = New-Object System.Collections.ArrayList
# Iterate over the direct children by index. The first child
# is at index 0.
for ($i = 0; $i -le $xml1.NumChildren - 1; $i++) {
$output = $("$i : $($xml1.GetChildTagByIndex($i)) : $($xml1.GetChildContentByIndex($i))")
Write-Host -ForegroundColor yellow "Top Level"
Write-Host -foregroundcolor green "Adding::::: $output"
$ResponseStrings.Add($output)
$xml2 = $xml1.GetChild($i)
for ($j = 0; $j -le $xml2.NumChildren -1; $j++) {
if ($xml2.GetChildContentByIndex($j) -eq $null -or $xml2.GetChildContentByIndex($j) -eq ""){
$xml3 = $xml2.GetChild($j)
for ($k = 0; $k -le $xml3.numchildren -1; $k++){
if ($xml3.GetChildContentByIndex($k) -eq $null -or $xml3.GetChildContentByIndex($k) -eq "") {
$xml4 = $xml3.GetChild($k)
for ($l = 0; $l -le $xml4.NumChildren -1; $l++) {
if ($xml4.GetChildContentByIndex($l) -eq $null -or $xml4.GetChildContentByIndex($l) -eq "") {
$xml5 = $xml4.GetChild($l)
for ($m = 0; $m -le $xml5.NumChildren -1; $m++){
if ($xml5.GetChildContentByIndex($m) -eq $null -or $xml5.GetChildContentByIndex($m) -eq "") {
$xml6 = $xml5.GetChild($m)
for ($n = 0; $n -le $xml6.NumChildren -1; $n++) {
$Output = $("$i : $($xml1.GetChildTagByIndex($i)) :: $($xml2.GetChildTagByIndex($j)) :: $($xml3.GetChildTagByIndex($k)) $($xml4.GetChildTagByIndex($l)) :: $($xml5.GetChildTagByIndex($m)) $($xml5.GetChildTagByIndex($m)) :: $($xml6.GetChildTagByIndex($n)) : $($xml6.GetChildContentByIndex($n)))")
Write-Host -ForegroundColor Green "adding::::: $output"
$ResponseStrings.Add($output)
}
}
$output = $("$i : $($xml1.GetChildTagByIndex($i)) :: $($xml2.GetChildTagByIndex($j)) :: $($xml3.GetChildTagByIndex($k)) $($xml4.GetChildTagByIndex($l)) :: $($xml5.GetChildTagByIndex($m)) : $($xml5.GetChildContentByIndex($m))")
Write-Host -ForegroundColor Green "adding::::: $output"
$ResponseStrings.Add($output)
}
}
$output = $("$i : $($xml1.GetChildTagByIndex($i)) :: $($xml2.GetChildTagByIndex($j)) :: $($xml3.GetChildTagByIndex($k)) :: $($xml4.GetChildTagByIndex($l)) : $($xml4.GetChildContentByIndex($l))")
Write-Host -ForegroundColor Green "adding::::: $output"
$ResponseStrings.Add($output)
}
}
$output = $("$i : $($xml1.GetChildTagByIndex($i)) :: $($xml2.GetChildTagByIndex($j)) :: $($xml3.GetChildTagByIndex($k)) : $($xml3.GetChildContentByIndex($k))")
Write-Host -ForegroundColor Green "adding::::: $output"
$ResponseStrings.Add($output)
}
}
$output = $("$i : $($xml1.GetChildTagByIndex($i)) :: $($xml2.GetChildTagByIndex($j)) : $($xml2.GetChildContentByIndex($j))")
Write-Host -ForegroundColor Green "adding::::: $output"
$ResponseStrings.Add($output)
}
}
return $ResponseStrings
}
如果有人能看一看并解释为什么数组中有这么多孤立对象,而我认为我正在添加到数组中,我将不胜感激。当您将某个对象添加到数组列表时,它会发出元素的索引:
PS C:\> $ArrayList = New-Object System.Collections.ArrayList
PS C:\> $ArrayList.Add("something")
0
PS C:\> $ArrayList.Add("somemore")
1
我假设这些整数就是您所说的函数输出的“孤儿”
您可以通过多种方式抑制来自ArrayList.Add()
的输出:
# cast to [void]
[void]$ResponseStrings.Add($output)
# assign to null
$null = $ResponseStrings.Add($output)
# pipe to null
$ResponseStrings.Add($output) | Out-Null
或者,使用非阵列列表的内容
使用简单数组:
[array]$ResponseStrings += $output
或:
$ResponseStrings=新对象系统.Collections.Generic.List[string]
对于($i=0;$i-le$xml1.NumChildren-1;$i++){
#这现在是安全的,List.Add()不会返回任何内容
$ResponseStrings.Add($output)
#……等等
}
哪些条目?你指的是哪些孤儿?所有的号码?嗨,马蒂亚斯,谢谢你的回复。它已经向我解释过了。我现在明白为什么会有这些数字了。在本例中,我选择使用列表方法,因为返回该方法并将其传递给下一个函数更有意义。实际上,这不会有什么区别,因为PowerShell管道将两者视为IEnumerable
。对int数组使用List
的真正理由是,它的填充速度更快,因为它的大小是动态的(而数组的大小是固定的,运行时引擎必须在每次对它执行+=
操作时调整/重新创建它)
$ResponseStrings = New-Object System.Collections.Generic.List[string]
for ($i = 0; $i -le $xml1.NumChildren - 1; $i++){
# this is now safe, List<string>.Add() doesn't return anything
$ResponseStrings.Add($output)
# ... and so on
}