创建一个锯齿形多维数组,其中包含单个数组 [英] Creating a Jagged\Multidimensional array with a single array inside of it

查看:112
本文介绍了创建一个锯齿形多维数组,其中包含单个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前遇到一个脚本问题,该脚本基于一组动态服务器(传递给它的优先级)构建阵列。例如,基于下面的输入

I'm currently having an issue with a script that builds an array based off of a dynamic set of servers\priorities that are passed to it. For example, based off of the input below

server1,200
server2,200

我想构建类似于

$arr=@() #at this point I have an empty array
$arr+=@("server1",200) #at this point, I would expect to have an array
                       #that itself holds another array

但是,当我运行this时,得到了意外的输出

However at this point, when I run the this I get unexpected output

echo $arr.count # result is 2, rather than the 1 I would expect
                # It appears to be treating $arr as a single array
                # with two members (server1 and 200) rather than an array
                # that holds an array, which itself has two members

但是,如果我向数组中添加另一个空数组:

However, if I add another empty array to my array:

$arr = @()
$arr += @()
$arr += $("server1",200)
$arr.count # output is 2, which is the desired result

我得到了期望的结果。我的问题是..我能得到一个锯齿形的多维数组,里面只有一个数组的理想结果吗?在大多数情况下,这不会是一种常见的情况,因为我要处理的是多套项目,但是我想考虑所有情况,并且可能会突然出现。我只想不必为此添加一个额外的步骤就可以过滤出一个空数组。

I get my desired result. My question is.. can I get my desired result of a jagged\multidimensional array with just a single array inside of it? This isn't going to be a common scenario as a majority of the time there will be multiple sets of items that I'm dealing with, however I'd like to account for all scenarios and this may be one that pops up. I'd just like to not have to add an additional step of filtering out an empty array just to satisfy this.

任何输入都将不胜感激。

Any input would be greatly appreciated.

推荐答案

您的2个示例将产生完全相同的结果,即带有2个元素的平面数组,因为附加了一个空数组( $ arr + = @())不会以任何方式修改数组。

Your 2 examples produce the exact same result, a flat array with 2 elements, because appending an empty array ($arr+=@()) does not modify the array in any way.

演示:

PS C:\> $arr = @()
PS C:\> $arr += @('server1', 200)
PS C:\> $arr.Count
2
PS C:\> Format-Custom -InputObject $arr
server1
200



PS C:\> $arr = @()
PS C:\> $arr += @()
PS C:\> $arr += @('server1', 200)
PS C:\> $arr.Count
2
PS C:\> Format-Custom -InputObject $arr
server1
200

使用append运算符时( + = )有两个数组PowerShell基本上将数组连接在一起。要将数组作为嵌套元素追加到第一个数组,您需要在第二个数组前加上一元数组构造运算符()。

When you use the append operator (+=) with two arrays PowerShell basically concatenates the arrays. To append an array as a nested element to the first array you need to prepend the second array with the unary array construction operator (,).

PS C:\> $arr = @()
PS C:\> $arr += ,@('server1', 200)
PS C:\> $arr += ,@('server2', 200)
PS C:\> $arr.Count
2
PS C:\> Format-Custom -InputObject $arr

[
  server1
  200
]

[
  server2
  200
]

如果事先知道元素的数量,还可以预先创建所需大小的数组,然后将嵌套的数组分配为其元素。

If you know the number of elements beforehand you could also pre-create an array of the desired size and assign the nested arrays as its elements. That doesn't require the comma operator.

PS C:\> $arr = New-Object Object[] 2
PS C:\> $arr.Count
2
PS C:\> $arr[0] = @('server1', 200)
PS C:\> $arr[1] = @('server2', 200)
PS C:\> Format-Custom -InputObject $arr

[
  server1
  200
]

[
  server2
  200
]

这篇关于创建一个锯齿形多维数组,其中包含单个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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