【问题标题】:Does powershell have associative arrays?powershell 有关联数组吗?
【发布时间】:2010-12-03 03:00:27
【问题描述】:

我正在编写一个返回 id、name 对的函数。

我想做类似的事情

$a = get-name-id-pair()
$a.Id
$a.Name

喜欢在 javascript 中是可能的。或者至少

$a = get-name-id-pair()
$a["id"]
$a["name"]

like 在 php 中是可能的。我可以用 powershell 做到这一点吗?

【问题讨论】:

    标签: arrays powershell associative-array


    【解决方案1】:

    从 JSON 字符串创建

    $people= '[
    {
    "name":"John", 
    "phone":"(555) 555-5555"
    },{
    "name":"Mary", 
    "phone":"(444) 444-4444"
    }
    ]';
    
    # Convert String To Powershell Array
    $people_obj = ConvertFrom-Json -InputObject $people;
    
    # Loop through them and get each value by key.
    Foreach($person in $people_obj ) {
        echo $person.name;
    }
    

    【讨论】:

      【解决方案2】:
      PS C:\> $a = @{}                                                      
      PS C:\> $a.gettype()                                                  
      
      IsPublic IsSerial Name                                     BaseType            
      
      -------- -------- ----                                     --------            
      
      True     True     Hashtable                                System.Object       
      

      所以哈希表是一个关联数组。呵呵。

      或者:

      PS C:\> $a = [Collections.Hashtable]::new()
      

      【讨论】:

        【解决方案3】:

        还将添加遍历哈希表的方法,因为我正在寻找解决方案但没有找到...

        $c = @{"1"="one";"2"="two"} 
        foreach($g in $c.Keys){write-host $c[$g]} #where key = $g and value = $c[$g]
        

        【讨论】:

        • 唯一提到分号;分隔符的答案。
        【解决方案4】:

        我在处理多个域时使用它来跟踪站点/目录。可以在声明数组时初始化数组,而不是单独添加每个条目:

        $domain = $env:userdnsdomain
        $siteUrls = @{ 'TEST' = 'http://test/SystemCentre' 
                       'LIVE' = 'http://live/SystemCentre' }
        
        $url = $siteUrls[$domain]
        

        【讨论】:

          【解决方案5】:
          #Define an empty hash
          $i = @{}
          
          #Define entries in hash as a number/value pair - ie. number 12345 paired with Mike is   entered as $hash[number] = 'value'
          
          $i['12345'] = 'Mike'  
          $i['23456'] = 'Henry'  
          $i['34567'] = 'Dave'  
          $i['45678'] = 'Anne'  
          $i['56789'] = 'Mary'  
          
          #(optional, depending on what you're trying to do) call value pair from hash table as a variable of your choosing
          
          $x = $i['12345']
          
          #Display the value of the variable you defined
          
          $x
          
          #If you entered everything as above, value returned would be:
          
          Mike
          

          【讨论】:

            【解决方案6】:

            还有

            $a = @{'foo'='bar'}
            

            $a = @{}
            $a.foo = 'bar'
            

            【讨论】:

            • 旧评论,但是如何通过 foreach 循环遍历关联数组?
            • $associativeArray=@{ Jane=1 Tom=2 Harry=3 } foreach($associativeArray.Keys 中的$key) { $key } foreach($associativeArray.GetEnumerator() 中的$item) { "{0}={1}" -f $item.Key, $item.Value }×评论至少15个字符×评论至少15个字符×评论至少15个字符长度。
            • $associativeArray=@{ Jane=1 Tom=2 Harry=3 } 给了我Unexpected token 'Tom=2' in expression or statement. 有效的是$associativeArray=@{ Jane=1; Tom=2; Harry=3 }
            • @stib,每个值必须要么在单独的行上(一个简单的 return 就可以了),或者像你一样使用分隔符。单独的行便于阅读。
            【解决方案7】:

            您也可以这样做:

            function get-faqentry { "meaning of life?", 42 }
            $q, $a = get-faqentry 
            

            不是关联数组,但同样有用。

            -奥辛

            【讨论】:

            • 更简单的是$q, $a = @("meaning of life?", 42)
            【解决方案8】:

            是的。使用以下语法创建它们

            $a = @{}
            $a["foo"] = "bar"
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2014-02-06
              • 2012-06-10
              • 1970-01-01
              • 2010-10-11
              • 2018-03-28
              • 1970-01-01
              • 2014-11-25
              • 2011-06-26
              相关资源
              最近更新 更多