gpt4 book ai didi

Powershell 添加到多维数组

转载 作者:行者123 更新时间:2023-12-04 01:37:56 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Powershell Multidimensional Arrays

(11 个回答)


5年前关闭。




我想在 powershell 中创建一个多维数组,如下所示:

$array[0] = "colours"
$array[0][0] = "red"
$array[0][1] = "blue"
$array[1] = "animals"
$array[1][0] = "cat"
$array[1][0] = "dog"

这是我尝试过的:
$array = @()
$array += "colours"
$array += "animals"

$array[0] # outputs "colours"
$array[1] # outputs "animals"

$array[0] = @()
$array[1] = @()

$array[0] += "red"
$array[0] += "blue"
$array[1] += "cat"
$array[1] += "dog"

$array[0] # outputs "red", "blue" - i expected "colours" here
$array[0][0] # outputs "red"

我很欣赏任何提示。

提前致谢

最佳答案

看起来你最好使用 [hashtable] (也称为关联数组):

$hash = @{
colours = @('red','blue')
animals = @('cat','dog')
}

$hash.Keys # show all the keys

$hash['colours'] # show all the colours
$hash.colours # same thing

$hash['colours'][0] # red

$hash['foods'] = @('cheese','biscuits') # new one
$hash.clothes = @('pants','shirts') #another way

$hash.clothes += 'socks'

关于Powershell 添加到多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40431840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com