Powershell 在填充 arrayList 时添加整数 [英] Powershell's adding Integers when populating arrayList
问题描述
我在 Powershell 中有一个函数,可以用字典填充列表.
I have a function in Powershell which populates a list with dictionaries.
Function Process-XML-Audit-File-To-Controls-List($nodelist){
#Keep an array list to track the different controls
$control_list = New-Object System.Collections.ArrayList
foreach($var in $nodelist)
{
$lines = [string]($var.InnerText) -split '[\r\n]'
$control_dict = @{}
foreach($line in $lines){
$line_split = $line.Trim() -split ':',2
if($line_split.Length -eq 2){
$control_dict.Add($line_split[0],$line_split[1])
}
}
$control_list.Add($control_dict)
}
return $control_list
}
它不是接收只返回哈希表的 ArrayList,而是返回一个包含 Int32 和 Hashtable 的列表,其中每个哈希表元素都有一个 Int32:
Instead of receiving an ArrayList which returns only Hashtables it returns a list which has Int32 and Hashtable, where there is an Int32 in it for each hashtable element:
True True Int32 System.ValueType
True True Int32 System.ValueType
True True Int32 System.ValueType
True True Hashtable System.Object
True True Hashtable System.Object
True True Hashtable System.Object
我不太确定为什么我的 ArrayList 中有这些整数.
I'm not really sure why I have those integers in my ArrayList.
推荐答案
这里的问题是 ArrayList.Add()
返回添加新项目的索引.当您return $control_list
时,表示索引位置的整数已经写入管道
The problem here is that ArrayList.Add()
returns the index at which the new item was added. When you return $control_list
, the integers representing the index locations have already been written to the pipeline
在方法调用前加上 [void]
以移除 Add()
的输出:
Prefix the method call with [void]
to remove the output from Add()
:
[void]$control_list.Add($control_dict)
或者管道到Out-Null
:
$control_list.Add($control_dict) | Out-Null
这篇关于Powershell 在填充 arrayList 时添加整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!