循环遍历哈希,或在 PowerShell 中使用数组
我正在使用这段(简化的)代码块通过 BCP。
$OutputDirectory = 'c:\junk\'
$ServerOption = "-SServerName"
$TargetDatabase = "Content.dbo."
$ExtractTables = @(
"Page"
, "ChecklistItemCategory"
, "ChecklistItem"
)
for ($i=0; $i -le $ExtractTables.Length – 1; $i++) {
$InputFullTableName = "$TargetDatabase$($ExtractTables[$i])"
$OutputFullFileName = "$OutputDirectory$($ExtractTables[$i])"
bcp $InputFullTableName out $OutputFullFileName -T -c $ServerOption
}
它工作得很好,但现在有些表需要通过视图提取,有些则不需要。所以我需要一个像这样的数据结构:
"Page" "vExtractPage"
, "ChecklistItemCategory" "ChecklistItemCategory"
, "ChecklistItem" "vExtractChecklistItem"
我正在查看哈希,但我没有找到有关如何循环哈希的任何内容。在这里做什么才是正确的?也许只使用一个数组,但两个值都用空格分隔?
或者我错过了一些明显的东西?
I'm using this (simplified) chunk of code to extract a set of tables from SQL Server with BCP.
$OutputDirectory = 'c:\junk\'
$ServerOption = "-SServerName"
$TargetDatabase = "Content.dbo."
$ExtractTables = @(
"Page"
, "ChecklistItemCategory"
, "ChecklistItem"
)
for ($i=0; $i -le $ExtractTables.Length – 1; $i++) {
$InputFullTableName = "$TargetDatabase$($ExtractTables[$i])"
$OutputFullFileName = "$OutputDirectory$($ExtractTables[$i])"
bcp $InputFullTableName out $OutputFullFileName -T -c $ServerOption
}
It works great, but now some of the tables need to be extracted via views, and some don't. So I need a data structure something like this:
"Page" "vExtractPage"
, "ChecklistItemCategory" "ChecklistItemCategory"
, "ChecklistItem" "vExtractChecklistItem"
I was looking at hashes, but I'm not finding anything on how to loop through a hash. What would be the right thing to do here? Perhaps just use an array, but with both values, separated by space?
Or am I missing something obvious?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
对于脚本来说,速记不是首选;它的可读性较差。 %{} 运算符被视为简写。为了提高可读性和可重用性,应该如何在脚本中完成此操作:
变量设置
选项 1:GetEnumerator()
注意:个人偏好;语法更容易阅读
GetEnumerator() 方法将如下所示完成:
输出:
选项 2:Keys
Keys 方法将如下所示完成:
输出:
附加信息
小心对哈希表进行排序...
Sort-Object 可能会将其更改为数组:
此和其他 PowerShell我的博客上提供循环播放。
Shorthand is not preferred for scripts; it is less readable. The %{} operator is considered shorthand. Here's how it should be done in a script for readability and reusability:
Variable Setup
Option 1: GetEnumerator()
Note: personal preference; syntax is easier to read
The GetEnumerator() method would be done as shown:
Output:
Option 2: Keys
The Keys method would be done as shown:
Output:
Additional information
Be careful sorting your hashtable...
Sort-Object may change it to an array:
This and other PowerShell looping are available on my blog.
Christian 的答案效果很好,并展示了如何使用 GetEnumerator 方法循环遍历每个哈希表项。您还可以使用
keys
属性进行循环。下面是一个示例:输出:
Christian's answer works well and shows how you can loop through each hash table item using the
GetEnumerator
method. You can also loop through using thekeys
property. Here is an example how:Output:
您也可以在不使用变量的情况下执行此操作
You can also do this without a variable
我更喜欢带有管道的枚举器方法的这种变体,因为您不必在 foreach 中引用哈希表(在 PowerShell 5 中测试):
输出:
现在,这还没有刻意按值排序,枚举器只是以相反的顺序返回对象。
但由于这是一个管道,我现在可以根据值对从枚举器接收到的对象进行排序:
输出:
I prefer this variant on the enumerator method with a pipeline, because you don't have to refer to the hash table in the foreach (tested in PowerShell 5):
Output:
Now, this has not been deliberately sorted on value, the enumerator simply returns the objects in reverse order.
But since this is a pipeline, I now can sort the objects received from the enumerator on value:
Output:
这是另一种快速方法,只需使用键作为哈希表的索引即可获取值:
Here is another quick way, just using the key as an index into the hash table to get the value:
关于循环哈希:
About looping through a hash:
也可以使用子表达式运算符 $( ) 进行短遍历,它返回一个或多个语句的结果。
结果:
A short traverse could be given too using the sub-expression operator $( ), which returns the result of one or more statements.
Result:
如果您使用的是 PowerShell v3,则可以使用 JSON 而不是哈希表,并将其转换为具有 Convert-FromJson:
If you're using PowerShell v3, you can use JSON instead of a hashtable, and convert it to an object with Convert-FromJson: