Manning----Powershell In Action
Page 71
 
In the previous section, we talked about hashtables and hash literals. Now let’s talk about the PowerShell syntax for arrays and array literals. Most programming languages have some kind of array literal notation similar to the PowerShell hash literal notation,where there is a beginning character sequence followed by a list of values, followed by a closing character sequence. Here’s how array literals are defined in PowerShell:
在前一节里,我们谈论了hashtables和hash.现在让我们来讨论arrays与array的参数。大多数编程语言都有与powershell类似的array标记方式,有一个开始字母列,紧跟着一个值得列表,又是字母序列等等。Powershell中是这样定义的:
They’re not. There is no array literal notation in PowerShell.
Powershell中并没有为array单独定义标记方法
Yes, you read that correctly. There is no array literal notation in PowerShell. So how exactly does this work? How do you define an inline array in a PowerShell script? Here’s how to do it: instead of having array literals, there is a set of operations that create collections as needed. In fact, collections of objects are created and discarded transparently throughout PowerShell. If you need an array, one will be created for you. If you need a singleton (or scalar) value, the collection will be unwrapped as needed.
使得,你看到的并没有错。在Powershell中并没有为array标记。那又是如何工作的呢?在脚本里又如何定义array呢?并不需要array字符,只需要创建需要的集合。事实上,集合里的对象由powershell创建或者删除。如果你需要一个array,那就会创建array,如果你需要一个单独的值,那就会按你的需要输出。
 
Collecting pipeline output as an array
The most common operation resulting in an array in PowerShell is collecting the output from a pipeline. When you run a pipeline that emits a sequence of objects and assign that output to a variable, it automatically collects the elements into an array,specifically into a .NET object of type [object[]].But what about building a simple array in an expression? The simplest way to do this is to use the comma operator (“,”). For example, at the command line, type
 1,2,3
在Powershell中常见的生成array的操作就是将一个管道命令的输出为一个集合。当你运行一个命令,得到了一系列的对象,把这些输出放入一个值,powershell会自动的收集元素放入array里,也就是一个.NET的对象类型pobject[]]。但如何在一个表达式里创建一个简单的array?最简单的方法就是用逗号,例如
1,2,3
and you’ll have created a sequence of numbers. (See chapter 5 for more information about using the comma operator.) When you assign that sequence to a variable, it is stored as an array. Let’s assign these three numbers to a variable $a and look at the type of the result.
当你把他赋予一个变量,就得到了数组,如下:
PS (1) > $a = 1,2,3
PS (2) > $a.gettype().fullname
System.Object[]
As in the pipeline case, the result is stored in an array of type [object[]].
 
Array indexing
Let’s explore some of the operations that can be performed on arrays. As is commonly the case, getting and setting elements of the array (array indexing) is done with square brackets. The length of an array can be retrieved with the Length property.

PS (3) > $a.length
3
PS (4) > $a[0]
1
Note that arrays in PowerShell are origin-zero; that is, the first element in the array is at index 0, not index 1. As the example showed, the first element of $a is in $a[0].As with hashtables, changes are made to an array by assigning new values to indexes in the array. In the following example, we’ll assign new values to the first and third elements in $a.
注意到Powershell是从0开始计数的,第一个元素的索引就是0而不是1、如例子中展示的那样,$a的第一个元素保存在$a[0]中。和hashtable一样,修改array的元素只需要赋予新值即可。
PS (5) > $a[0] = 3.1415
PS (6) > $a
3.1415
2
3
PS (7) > $a[2] = "Hi there"
PS (8) > $a
3.1415
2
Hi there
 
Polymorphism in arrays
Another important thing to note from the previous example is that arrays are polymorphic by default. By polymorphic we mean that you can store any type of object in an array. (A VBScript user would call these variant arrays). When we created the array,we assigned only integers to it. In the subsequent assignments, we assigned a floatingpoint number and a string. The original array was capable of storing any kind of object. In formal terms, PowerShell arrays are polymorphic by default (though it is possible to create type-constrained arrays). Earlier we saw how to get the length of an array. What happens when we try to assign to an element past the end of the array?
在前一个例子中另一个需要关注的东西就是array在默认情况下是polymorphic的。也就是是说你可以在一个array中能够储存多种格式的对象,可以同时保存整数和浮点数。Powershell也能储存某种特定类型的array。先前我们看到了如何获得array的长度,现在让我们看看当把一个值赋予给未定义的索引时会发生什么
The next example illustrates this.
PS (9) > $a.length
3
PS (10) > $a[4] = 22
Array assignment failed because index '4' was out of range.
At line:1 char:4
+ $a[4 <<<< ] = 22
PS (11) >
Attempts to assign outside the bounds of an array will result in a range error. This is because PowerShell arrays are based on .NET arrays and they are of fixed size. So how can I add more elements to a PowerShell array if the underlying objects are fixed in size? In fact, this is easily done through array concatenation using the plus (“+”) or plus-equals (“+=”) operators. Let’s add two more elements to the array from the previous example.
尝试在array边界之外添加会导致错误,这是因为Powershell的array是基于.NET的array,是锁定大小的。因此想要添加,就得使用+或者+=算子
PS (11) > $a += 22,33
PS (12) > $a.length
5
PS (13) > $a[4]
33
PS (14) >