【问题标题】:Powershell Array is printing the item index along with valuesPowershell Array 正在打印项目索引以及值
【发布时间】:2021-03-22 12:27:33
【问题描述】:

我在 Powershell 中返回数组。但作为回应,我得到了数组值以及相应值的索引。

如何阻止项目索引在控制台中打印。

代码示例:

$data = [System.Collections.ArrayList]@()
$Item1 = "vimal" +"::"+ "error1"      
$data.add($Item1)
$Item1 = "vimal" +"::"+ "error2"      
$data.add($Item1)

return $data

回复:

0 1 vimal::error1 vimal::error2

我不想在控制台中打印 0 和 1。

谢谢, 维马尔

【问题讨论】:

    标签: arrays powershell powershell-ise


    【解决方案1】:

    ArrayList.Add 返回新添加对象的索引。这些返回(所有未捕获的返回)被连接在一起

    $data = [System.Collections.ArrayList]@()
    $Item1 = "vimal" +"::"+ "error1"      
    $data.add($Item1)   # <<== This outputs "0" because Add() returns index (0) of added object
    $Item1 = "vimal" +"::"+ "error2"      
    $data.add($Item1)   # <<== This outputs "1"
    
    return $data  # <<== This outputs "..error1" and "..error2"
    

    要抑制不需要的返回,请使用[void]$data.add(...)$data.add(...) | Out-Null 或为某个变量赋值:$indexOfItem1 = $data.add(...)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-20
      • 1970-01-01
      • 1970-01-01
      • 2020-10-18
      • 2017-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多