我想出了数组,但是现在我想把我的$ 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])
我该如何解决这个问题?还是有更聪明的方法? 简而言之,我想在上面显示的Grid中包含数组的位置值。
编辑:
$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"
}
}
答案 0 :(得分:0)
当您将表达式放在双引号字符串中时,解析器将停在第一个非变量名称字符处。所以:
"$CreateGrid[1,1]"
被处理为好像是
"$CreateGrid" + "[1,1]"
并且$CreateGrid
是(或者至少看起来是)一个数组,它在每个成员上执行ToString
并连接结果(因为这是一个二维数组,每个枚举成员是一个数组,因此多次System.Object[]
。
如果在字符串中使用表达式语法($(...)
),则整个包含的表达式将作为PowerShell表达式处理(例如,您可以在其中放置管道):
"$($CreateGrid[1,1])"