0

我想出了数组,但现在我想写入我的 $CreateGrid[1,1] 的值,例如

Write-host "  A   B   C   D   E   F   G   H   I   J " 
Write-host "+---+---+---+---+---+---+---+---+---+---+ "
Write-host "|   |   |   |   |   |   |   |   |   |   | 1"
Write-host "+---+---+---+---+---+---+---+---+---+---+ "
Write-host "|   |   |   |   |   |   |   |   |   |   | 2"
Write-host "+---+---+---+---+---+---+---+---+---+---+ "
Write-host "|   |   |   |   |   |   |   |   |   |   | 3"
Write-host "+---+---+---+---+---+---+---+---+---+---+ "
Write-host "|   |   |   |   |   |   |   |   |   |   | 4"
Write-host "+---+---+---+---+---+---+---+---+---+---+ "
Write-host "|   |   |   |   |   |   |   |   |   |   | 5"
Write-host "+---+---+---+---+---+---+---+---+---+---+ "
Write-host "|   |   |   |   |   |   |   |   |   |   | 6"
Write-host "+---+---+---+---+---+---+---+---+---+---+ "
Write-host "|   |   |   |   |   |   |   |   |   |   | 7"
Write-host "+---+---+---+---+---+---+---+---+---+---+ "
Write-host "|   |   |   |   |   |   |   |   |   |   | 8"
Write-host "+---+---+---+---+---+---+---+---+---+---+ "
Write-host "|   |   |   |   |   |   |   |   |   |   | 9"
Write-host "+---+---+---+---+---+---+---+---+---+---+ "
Write-host "| $CreateGrid[1,1] |   |   |   |   |   |   |   |   |   | 10"
Write-host "+---+---+---+---+---+---+---+---+---+---+ " 

但是,当我尝试这个时,我得到以下值的输出:

(          System.Object[] System.Object[] System.Object[] System.Object[] System.Object[] System.Object[] System.Object[] System.Object[]             Syst
em.Object[] System.Object[]                   System.Object[]                                                 [1,1])

我将如何解决这个问题?还是有更聪明的方法?简而言之,我想在上面显示的网格中包含数组的位置值。

编辑:

$CreateBoard = New-object "Array[,]" 10,10

Function Add-ToColumn{
param ([Int] $columnnum,[String] $player)

    PROCESS{if (0..9 -notcontains $columnnum){"Invalid move";return}
        #0 is the bottom, 9 is the top
        for($i = 0; $i -le 9; $i++)
        {
            if ($CreateBoard[$columnnum, $i] -eq $null)
            {
                $CreateBoard[$columnnum, $i] = $player
                "Coin placed in $columnnum, $i coins in the column!"
                return
            }
        }
        #if you get here, column is full
        "Invalid move"
    }
}
4

1 回答 1

0

当您将表达式放在双引号字符串中时,解析器会在第一个非变量名称字符处停止。所以:

"$CreateGrid[1,1]"

被处理就好像它是

"$CreateGrid" + "[1,1]"

并且作为$CreateGrid(或至少看起来是)一个数组,它对ToString每个成员执行 a 并连接结果(因为这是一个二维数组,每个枚举成员都是一个数组,因此System.Object[]多次)。

如果您$(...)在字符串中使用表达式语法 ( ),则整个包含的表达式将作为 PowerShell 表达式处理(例如,您可以在其中放置一个管道):

"$($CreateGrid[1,1])"
于 2013-01-16T10:40:37.377 回答