如何解决在简单的数组操作期间,powershell无法索引到空数组
我是PowerShell的新手,遇到了问题。我尝试通过使用格式“ x,y,b”(其中x和y是颜色为b的国际象棋的平面坐标)来制作一个Powershell脚本,该脚本读取一个描述国际象棋位置的txt文件。>
我在ps中编写了以下代码:
function makeboard2d($file){
$blankline=-1,-1,-1
$board2d=$blankline,$blankline,$blankline
$board=Get-Content $file
for ($i=0;$i -le $board.Length;$i++){
$x=$board[$i][0]
$y=$board[$i][2]
$b=$board[$i][4]
$board2d[$x][$y]=$b
}
}
makeboard2d("G:\board.txt")
和txt文件G:\ board.txt具有以下内容,格式为“ x,y,b”:
1,2,1
3,3,0
我首先想做的是构造一个空白的15 x 15棋盘($ board2d),将每个条目设置为-1,这意味着没有放置象棋。然后我需要从G:\ board.txt中读取输入行(例如1,1),即玩家A和B的交替走动,并使用此类输入来修改上述的二维棋盘$ board2d。这里的错误是从循环内$ x = $ board [$ i] [0]发生的。
您能提供一些指导吗?
真的谢谢你。
解决方法
您在这里遇到很多问题。
-
如果按原样创建board2d,它将更改同一列中的所有元素(如果设置了col 2,则将为所有行设置col 2)
-
$x,$y,$b
由字符(0、2和4)定义。这样做有2个问题,首先您的电路板是15x15,因此对于第11行/第11行及以后,它需要2个字符。其次,如果您使用字符,$x,$b
会为此字符设置为ascii值,而不是实际值 -
您的循环太过
-lt
了function makeboard2d($file){ $board2d = @() for ([int]$i = 0; $i -lt 15; $i++) { $board2d +=,@(-1,-1,-1) } $board=Get-Content $file for ($i=0;$i -lt $board.Length;$i++){ $values = $board[$i] -split ',' $x=$values[0] # Index here is for an array value,not a character $y=$values[1] $b=$values[2] $board2d[$x][$y]=$b } }
Get-Content
cmdlet以一维字符串的一维数组的形式读取文件。
如果您希望将其变成多维数组,则可以
# initialize board to an array of arrays with all -1 values:
$board2d = for ($i = 0; $i -lt 15; $i++) {,-1) }
# set board to whatever is in the file
$file = 'D:\Test\board.txt'
$board2d = Get-Content -Path $file
for ($i = 0; $i -lt $board2d.Count; $i++) { $board2d[$i] = ($board2d[$i] -split ',') }
所有数组值都是字符串类型。如果出于计算目的需要将它们用作数字,请执行以下操作:
for ($i = 0; $i -lt $board2d.Count; $i++) { $board2d[$i] = [int[]]($board2d[$i] -split ',') }
按索引引用不同的值(索引从0开始)
# $board2d[0][0] --> first element in the array,X value
# $board2d[0][1] --> first element in the array,Y value
# $board2d[0][2] --> first element in the array,Color value
# $board2d[1][0] --> second element in the array,X value
# $board2d[1][1] --> second element in the array,Y value
# $board2d[1][2] --> second element in the array,Color value
但是,使用具有命名属性的对象数组可能会更舒适:
# initialize board to an array of objects with all properties set to -1:
$board2d = for ($i = 0; $i -lt 15; $i++) { [PsCustomObject]@{X = -1; Y = -1; Color = -1} }
# set board to whatever is in the file
$file = 'D:\Test\board.txt'
$board2d = Import-Csv -Path $file -Header X,Y,Color
再次,从文件中读取的所有值均为字符串类型。要转换为整数,您可以执行以下操作:
$board2d = Import-Csv -Path $file -Header X,Color |
Select-Object @{Name = 'X'; Expression = { [int]$_.X}},@{Name = 'Y'; Expression = { [int]$_.Y}},@{Name = 'Color'; Expression = { [int]$_.Color}}
现在您可以通过属性名称引用不同的值
# $board2d[0].X --> first element in the array,X value
# $board2d[0].Y --> first element in the array,Y value
# $board2d[0].Color --> first element in the array,Color value
# $board2d[1].X --> second element in the array,X value
# $board2d[1].Y --> second element in the array,Y value
# $board2d[1].Color --> second element in the array,Color value
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。