我试图构造一个字符串数组的数组,但是如果我创建一个只有一个子数组的数组,它似乎会以某种方式变成字符数组的数组,这是一个问题。
但是如果我传入2个或更多的子数组,无论是硬编码的还是来自变量的,它都会按预期工作
我需要将其传递到一个自定义函数中,然后迭代该函数,但由于单个子数组的奇怪方式,它完全失败
编辑:由于这显然仍然不够容易理解这里的更多信息,还错过了一些测试代码,其中包括4和5测试,我已经修复.
如果您查看测试4a和4b,这是字符串的单个子数组的结果,您可以看到它没有按预期工作,应该发生的是,4a应该与5a相同,4b应该创建一个索引越界异常。
下面是我的测试代码:
$Maps = [string[]]::new(5)
$Maps[0] = 'Ireland'
$Maps[1] = 'Scotland'
$Maps[2] = 'England'
$Maps[3] = 'Germany'
$Maps[4] = 'France'
$Maps2 = [string[]]::new(5)
$Maps2[0] = 'Japan'
$Maps2[1] = 'Crete'
$Maps2[2] = 'USA'
$Maps2[3] = 'Canada'
$Maps2[4] = 'Greece'
$Array = @($Maps)
Write-Host 'These results seem to treat a single variable as character arrays?'
Write-Host Test 1a: $Array[0][0]
Write-Host Test 1a: $Array[0][1]
Write-Host Test 1b: $Array[1][0]
Write-Host Test 1b: $Array[1][1]
Write-Host Test 1c: $Array[2][0]
Write-Host Test 1c: $Array[2][1]
$Array = @($Maps, $Maps2)
Write-Host 'These results seem to create the correct results'
Write-Host Test 2a: $Array[0][0]
Write-Host Test 2a: $Array[0][1]
Write-Host Test 2b: $Array[1][0]
Write-Host Test 2b: $Array[1][1]
Write-Host Test 2c: $Array[2][0]
Write-Host Test 2c: $Array[2][1]
$Array = @($Maps, @('Test1', 'test2'))
Write-Host 'These results seem to create the correct results'
Write-Host Test 3b: $Array[0][0]
Write-Host Test 3b: $Array[0][1]
Write-Host Test 3c: $Array[1][0]
Write-Host Test 3c: $Array[1][1]
Write-Host Test 3d: $Array[2][0]
Write-Host Test 3d: $Array[2][1]
$Array = @(@('Available Maps', 'Scotland', 'England', 'Germany', 'France'))
Write-Host 'Same Issue as First Example'
Write-Host Test 4a: $Array[0][0]
Write-Host Test 4a: $Array[0][1]
Write-Host Test 4b: $Array[1][0]
Write-Host Test 4b: $Array[1][1]
$Array = @(@('Available Maps', 'Scotland', 'England', 'Germany', 'France'), @('Test1', 'test2'))
Write-Host 'Works as Expected'
Write-Host Test 5a: $Array[0][0]
Write-Host Test 5a: $Array[0][1]
Write-Host Test 5b: $Array[1][0]
Write-Host Test 5b: $Array[1][1]
以下是结果
These results seem to treat a single variable as character arrays?
Test 1a: I
Test 1a: r
Test 1b: S
Test 1b: c
Test 1c: E
Test 1c: n
These results seem to create the correct results
Test 2a: Ireland
Test 2a: Scotland
Test 2b: Japan
Test 2b: Crete
Cannot index into a null array.
At line:34 char:5
+ Write-Host Test 2c: $Array[2][0]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
Cannot index into a null array.
At line:35 char:5
+ Write-Host Test 2c: $Array[2][1]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
These results seem to create the correct results
Test 3b: Ireland
Test 3b: Scotland
Test 3c: Test1
Test 3c: test2
Cannot index into a null array.
At line:45 char:5
+ Write-Host Test 3d: $Array[2][0]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
Cannot index into a null array.
At line:46 char:5
+ Write-Host Test 3d: $Array[2][1]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
Same Issue as First Example
Test 4a: A
Test 4a: v
Test 4b: S
Test 4b: c
Works as Expected
Test 5a: Available Maps
Test 5a: Scotland
Test 5b: Test1
Test 5b: test2
谢啦,谢啦
2条答案
按热度按时间uyto3xhc1#
在示例
4a
和4b
中,如果您希望数组具有2个嵌套数组,则可以使用内部空数组子表达式运算符@()
:如果希望上面的示例抛出一个 out of bounds 异常,则应使用
$null
:使用逗号操作符
,
,您可以在父数组的**index0
**处创建嵌套数组:请注意,在所有这些示例中,不需要父
@()
,PowerShell可以动态地为您假设这一点:就我个人而言,我认为hash tables更适合于此。例如,第一个代码片段将是:
c6ubokkw2#
总的来说,我不明白为什么人们更喜欢使用哈希表(键+值)或嵌套对象(多个信息)而不是简单的老式数组(只有值)-也许我有点过时了?
$collection包含多个$item,每个$item都有多个键+值。
$arrayIdentifier将是一个很好的简单的经典数组,具有两列和多行,可通过以下方式访问:
PowerShell创建和添加数组的方式与其他语言相比非常粗糙。