【问题标题】:PowerShell dictionary of arrays?PowerShell的数组字典?
【发布时间】:2016-10-21 21:26:22
【问题描述】:

在 VBScript 中我可以做这样的事情

Set objDict = CreateObject("Scripting.Dictionary")
objDict.Add "item1", Array("data1", "data2")
objDict.Add "item2", Array("data3", "data4")

然后我可以使用类似的东西进行查找

dataArray = objDict.Item("item2")
elem1 = dataArray(0)
elem2 = dataArray(1)

结果是elem1 包含“data3”,elem2 包含“data4”

我不确定如何在 PowerShell 中复制它。有人帮忙吗?

【问题讨论】:

    标签: arrays powershell dictionary


    【解决方案1】:

    字典在 PowerShell 中称为哈希表。当 PowerShell 首次问世时,Microsoft 还发布了 VBScript-to-PowerShell Conversion Guide,其中包括 dictionariesarrays

    您可以这样定义哈希表(字典):

    $d = @{
      'foo' = 'something'
      'bar' = 42
    }
    

    或者——例如,如果您需要动态填充哈希表——您可以创建一个空的哈希表并向其中添加元素,如下所示:

    $d = @{}
    $d.Add('foo', 'something')
    $d.Add('bar', 42)
    

    或者像这样:

    $d = @{}
    $d['foo'] = 'something'
    $d['bar'] = 42
    

    通常我更喜欢后者,因为它会替换现有的键而不是抛出错误。

    $d1 = @{}
    $d1.Add('foo', 23)
    $d1.Add('foo', 42)   # can't add another key with same name => error
    
    $d2 = @{}
    $d2['foo'] = 23      # key is automatically added
    $d2['foo'] = 42      # replaces value of existing key
    

    数组被定义为一个简单的逗号分隔列表:

    $a = 'a', 'b', 'c'
    

    您也可以选择使用array subexpression operator:

    $a = @('a', 'b', 'c')
    

    如果您需要一个数组结果,但不确定您的表达式会产生多少个结果,这很有用。如果没有运算符,您将得到$null、单个值或数组,具体取决于表达式的结果。通过使用该运算符,您始终可以得到一个包含零个、一个或多个元素的数组。

    数组和哈希表元素都可以通过index operator ([]) 访问:

    $d['foo']  # output: "something"
    $a[1]      # output: "b"
    

    哈希表的元素也可以通过点符号访问:

    $d.foo     # output: "something"
    

    当然,您可以在哈希表中嵌套数组,反之亦然,就像在 VBScript 中一样。您的示例在 PowerShell 中看起来有点像这样

    $dict = @{
      'item1' = 'data1', 'data2'
      'item2' = 'data3', 'data4'
    }
    
    $dataArray = $dict['item2']
    $elem1 = $dataArray[0]
    $elem2 = $dataArray[1]
    

    不过,PowerShell 从其他脚本语言中学到了一两件事。例如,您可以将数组分配给变量列表,因此上述将数组元素分配给各个变量的方法可以简化为:

    $elem1, $elem2 = $dict['item2']
    

    如果数组的元素多于赋值左侧的变量数,则最后一个变量取数组的剩余部分:

    $x, $y, $z = 'a', 'b', 'c', 'd'
    # $x -> 'a'
    # $y -> 'b'
    # $z -> @('c', 'd')
    
    $x, $y, $z = 'a', 'b'
    # $x -> 'a'
    # $y -> 'b'
    # $z -> $null
    

    索引运算符允许访问多个元素:

    $a = 'a', 'b', 'c', 'd', 'e'
    $a[1,3,4]  # output: "b", "d", "e"
    

    以及数组末尾的元素:

    $a = 'a', 'b', 'c', 'd', 'e'
    $a[-1]  # output: "e"
    $a[-2]  # output: "d"
    

    range operator (..) 允许您通过指定第一个和最后一个索引从数组中获取切片。它产生一个从第一个操作数开始到第二个操作数结束的数字列表(例如2..52,3,4,5)。

    $a = 'a', 'b', 'c', 'd', 'e'
    $a[1..3]    # equivalent to $a[1,2,3], output: "b", "c", "d"
    $a[-1..-3]  # equivalent to $a[-1,-2,-3], output: "e", "d", "c"
    

    【讨论】:

    • 很棒的概述;值得一提:哈希表文字键仅在包含空格或元字符时才需要引用。
    • 同样值得注意的是,$dictionary.Add("foo", 42)$d['bar'] = 42 的行为非常不同,因为它们是非常不同的东西。
    • @AustinFrench 这就是相应示例中的 cmets 所要指出的。
    【解决方案2】:

    另一种解决方案,您可以像这样使用System.Collections.Generic.Dictionary

     #for create your dictionary
     $mydico = New-Object 'System.Collections.Generic.Dictionary[string,string]'
    
     #For add element into your dictionary
     $mydico.Add("mykey1", "myvalue1") 
     $mydico.Add("mykey2", "myvalue2")
    
     #for get value by key
     $value = $mydico["mykey1"]   # $value has myvalue1 like value
    

    【讨论】:

      猜你喜欢
      • 2023-01-24
      • 1970-01-01
      • 2020-05-02
      • 1970-01-01
      • 1970-01-01
      • 2021-12-15
      • 2023-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多