Powershell的填充ArrayList中添加时整数 [英] Powershell's adding Integers when populating arrayList

查看:344
本文介绍了Powershell的填充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
}

而不是接收仅返回哈希表返回拥有的Int32和Hashtable,那里是一个Int32在它为每​​个哈希表元素的列表中的一个ArrayList的:

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() 返回在其位置添加新项目的索引。当你收益$ control_list ,整数重新presenting指数位置已经被写入管道

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

preFIX与方法调用[空] 来删除输出添加()

Prefix the method call with [void] to remove the output from Add():

[void]$control_list.Add($control_dict)

或管道,以外空

$control_list.Add($control_dict) | Out-Null

这篇关于Powershell的填充ArrayList中添加时整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆