Powershell: Two dimension arrays

后端 未结 1 1301
别那么骄傲
别那么骄傲 2021-02-05 21:32

The following works as expected:

$values = @( (\"a\", \"b\"), (\"c\", \"d\") )
foreach($value in $values)
{
  write-host \"Value 0 =\" $value[0]
  write-host \"V         


        
相关标签:
1条回答
  • 2021-02-05 22:03

    This is another case where PowerShell's array handling behavior may cause unexpected results.

    I think you'll need to use the comma trick (the array operator) to get the desired result:

    $values = @( ,("a", "b") )
    foreach($value in $values)
    {
      write-host "Value 0 =" $value[0]
      write-host "Value 1 =" $value[1]
    }
    

    Result:

    Value 0 = a
    Value 1 = b
    

    Actually all you need is this:

    $values = ,("a", "b")
    

    This article explains more about PowerShell's handling of arrays:

    http://blogs.msdn.com/b/powershell/archive/2007/01/23/array-literals-in-powershell.aspx

    0 讨论(0)
提交回复
热议问题