【问题标题】:Create Object in Specific loaction in Array Powershell [duplicate]在Array Powershell的特定位置创建对象[重复]
【发布时间】:2013-11-13 23:53:11
【问题描述】:

我想知道你能不能帮我解决这个问题。我在 powershell 中有一个对象数组,其中包含:

$array = @(1,2,3,4,5)

所以$array 给了我这个:

1
2
3
4
5

现在我想在$array[3], 的位置添加数字 6,这样输出将是:

1
2
3
6
4
5

【问题讨论】:

    标签: arrays object powershell


    【解决方案1】:

    有很多方法可以做到这一点。例如。

    PS > $i = 1..5
    
    PS > $i
    #ouput
    1
    2
    3
    4
    5
    
    PS > function insertInto ($array, $index, $value) {
        @($array[0..($index-1)],$value,$array[$index..($array.length-1)])
    }
    
    PS > $i = insertInto $i 3 6
    
    PS > $i
    #output
    1
    2
    3
    6
    4
    5
    

    警告,上面的方法对于单值数组不是很好。

    【讨论】:

      猜你喜欢
      • 2020-09-16
      • 2019-01-18
      • 2012-11-09
      • 2012-05-16
      • 1970-01-01
      • 1970-01-01
      • 2018-04-19
      • 1970-01-01
      • 2017-04-05
      相关资源
      最近更新 更多