Powershell学习笔记3——has
短信预约 -IT技能 免费直播动态提醒
Manning--Powershell In Action
Page 66
Collections:dicitonaries and hashtables
One of the most flexible datatypes supported in PowerShell is the hashtable. This atatype lets you map a set of keys to a set of values. For example, we may have a hashtable that maps “red” to 1, “green” to 2, and “yellow” to 4.
在Powershell中hashtable是一种非常方便的数据类型。这种数据类型允许你在一堆键和值之间建立映射关系。例如将红色映射为1,绿色映射为2等等。
Creating and inspecting hashtables
In Powershell,you use hash literals to create a hashtable inline a script.Here it is a simple example:
This example created a hashtable that contained three key-value pairs. The hashtable starts with the token “@{” and ends with “}”. Inside the delimiters, you define a set of key-value pairs where the key and value are separated by an equals sign “=”. Formally,the syntax for a hash literal is
例子中创建了一个hashtable,包含三个键值对。hashtable以“@{”开始,以“}”结束。在分号里,你定义了一套键值对,用等号表示。参数包括:
例子中创建了一个hashtable,包含三个键值对。hashtable以“@{”开始,以“}”结束。在分号里,你定义了一套键值对,用等号表示。参数包括:
<hashLiteral> = '@{ <keyExpression> '=' <pipeline> [ <separator>
<keyExpression> '=' <pipeline> ] * '}'
<separator> = ';' | <newline>
<keyExpression> '=' <pipeline> ] * '}'
<separator> = ';' | <newline>
Now that we’ve created a hashtable, let’s see how we can use it. PowerShell allows you to access members in a hashtable in two ways—through property notation and through array notation.
现在我们已经创建了一个hashtable,让我们看看如何使用。Powershell允许你使用两种办法来获取数据——通过property标记或者array标记。
Here’s what the property notation looks like:这就是property标记的样子:
PS (3) > $user.firstname
John
PS (4) > $user.lastname
Smith
This notation lets you treat a hashtable like an object. This access method is intended
to facilitate the use of hashtables as a kind of lightweight data record. Now let’s look
at using the array notation.
John
PS (4) > $user.lastname
Smith
This notation lets you treat a hashtable like an object. This access method is intended
to facilitate the use of hashtables as a kind of lightweight data record. Now let’s look
at using the array notation.
标记让你可以像处理对象一样处理hashtable。这使得对hashtable的使用变得十分简便,就像是轻量级的数据记录。现在在让我们来看一下array标记:
PS (5) > $user["firstname"]
John
PS (6) > $user["firstname","lastname"]
John
Smith
John
PS (6) > $user["firstname","lastname"]
John
Smith
Property notation works pretty much the way you’d expect; you specify a property name and get the corresponding value back. Array notation, on the other hand, is more interesting. In the second command in the example, we provided two keys and got two values back.
Array标记比较Property标记更加有趣,在例子中,我们提供两个key,可以获得两个值
Here’s an example that shows some additional features of the underlying hashtable object. The underlying object for PowerShell hashtables is the .NET type System.Collections.Hashtable. This type has a number of properties and methods that you can use. One of these properties is the keys property. This property will give you a list of all of the keys in the hashtable.
下面这个例子中,我会展现一些hashtable隐藏的特性。Powershell中hashtable的隐藏对象是.NET中的System.collections.hashtable。这种类型有一些property和method可以使用。属性之一就是keys属性。这个属性会提供一份hashtable中的keys的列表
PS (7) > $user.keys
LastName
FirstName
PhoneNumber
LastName
FirstName
PhoneNumber
In the array access notation, you can use keys to get a list of all of the values in the table.
PS (8) > $user[$user.keys]
Smith
John
555-1212
PS (8) > $user[$user.keys]
Smith
John
555-1212
You might have noticed that the keys property didn’t return the keys in alphabetical order. This is because of the way hashtables work; i.e., keys are randomly distributed in the table to speed up access. If you do need to get the values in alphabetical order,here’s how you can do it:
你可能已经注意到,keys属性并不以字母表顺序返回值。这和hashtable的工作方式有关;例如,keys是随机地分布在表里,以加速读取。如果你需要以字母表顺序读取值,你可以这么做:
PS (10) > $user.keys | sort-object
FirstName
LastName
PhoneNumber
FirstName
LastName
PhoneNumber
让我们以这个顺序来列出值
PS (11) > $user[[string[]] ($user.keys | sort)]
John
Smith
555-1212
John
Smith
555-1212
You’ll notice something funny about the last example: we had to cast or convert the sorted list into an array of strings. This is because the hashtable keys mechanism expects strings, not objects, as keys. There’s much more on casts later in this chapter.
你会注意到最后一个例子中有些意思:我们不得不将分类号的列表转换到一个字符串的组。这是因为hashtable的keys机制需要字符类型,而不是对象。在这篇中还会有更多类似的况
Modifying and manipulating hashtables
Now let’s look at adding, changing, and removing elements from the hashtable. First let’s add the date and the city where the user lives to the $user table.
现在让我看看如何添加、修改和删除hashtable中的元素
PS (1) > $user.date = get-date
PS (2) > $user
Key Value
--- -----
LastName Smith
date 1/15/2006 12:01:10 PM
FirstName John
PhoneNumber 555-1212
PS (2) > $user
Key Value
--- -----
LastName Smith
date 1/15/2006 12:01:10 PM
FirstName John
PhoneNumber 555-1212
PS (3) > $user["city"] = "Seattle"
PS (4) > $user
Key Value
--- -----
city Seattle
LastName Smith
date 1/15/2006 12:01:10 PM
FirstName John
PhoneNumber 555-1212
PS (4) > $user
Key Value
--- -----
city Seattle
LastName Smith
date 1/15/2006 12:01:10 PM
FirstName John
PhoneNumber 555-1212
修改:
PS (5) > $user.city = "Detroit"
PS (6) > $user
Key Value
--- -----
city Detroit
LastName Smith
date 1/15/2006 12:01:10 PM
FirstName John
PhoneNumber 555-1212
PS (6) > $user
Key Value
--- -----
city Detroit
LastName Smith
date 1/15/2006 12:01:10 PM
FirstName John
PhoneNumber 555-1212
删除:
PS (7) > $user.remove("city")
PS (8) > $user
Key Value
--- -----
LastName Smith
date 1/15/2006 12:01:10 PM
FirstName John
PhoneNumber 555-1212
PS (8) > $user
Key Value
--- -----
LastName Smith
date 1/15/2006 12:01:10 PM
FirstName John
PhoneNumber 555-1212
Hashtables as reference types
Hashtables are reference types, so if you create a hashtable, assign it to a variable $foo,and assign $foo to another variable $bar, you will have two variables that point to or reference the same object. Consequently, any changes that are made to one variable will affect the other, because they’re pointing to the same object. Let’s try this out.Create a new hashtable and assign it to $foo.
Hashtables are reference types, so if you create a hashtable, assign it to a variable $foo,and assign $foo to another variable $bar, you will have two variables that point to or reference the same object. Consequently, any changes that are made to one variable will affect the other, because they’re pointing to the same object. Let’s try this out.Create a new hashtable and assign it to $foo.
Hashtable是引用类型,所以如果你创建了hashtable,可以把它赋予给变量$foo,并把$foo赋予给另一个变量$bar,你就得到了两个变量指向了同一个对象。因此,如何对一个变量的变化会影响到另一个变量。让我们尝试一下,建立一个新的hashtable并赋予给$foo.
PS (2) > $foo = @{
>> a = 1
>> b = 2
>> c = 3
>> }
>>
PS (3) > $foo
Key Value
--- -----
a 1
b 2
c 3
Now assign $foo to $bar and verify that it matches $foo as we expect.
PS (4) > $bar = $foo
PS (5) > $bar
Key Value
--- -----
a 1
b 2
c 3
Next assign a new value to the element "a" in $foo.
PS (6) > $foo.a = "Hi there"
PS (7) > $foo.a
Hi there
And let’s look at what happened to $bar:
PS (8) > $bar.a
Hi there
PS (9) > $bar
Key Value
--- -----
a Hi there
b 2
c 3
The change that was made to $foo has been reflected in $bar.
>> a = 1
>> b = 2
>> c = 3
>> }
>>
PS (3) > $foo
Key Value
--- -----
a 1
b 2
c 3
Now assign $foo to $bar and verify that it matches $foo as we expect.
PS (4) > $bar = $foo
PS (5) > $bar
Key Value
--- -----
a 1
b 2
c 3
Next assign a new value to the element "a" in $foo.
PS (6) > $foo.a = "Hi there"
PS (7) > $foo.a
Hi there
And let’s look at what happened to $bar:
PS (8) > $bar.a
Hi there
PS (9) > $bar
Key Value
--- -----
a Hi there
b 2
c 3
The change that was made to $foo has been reflected in $bar.
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341