如何制作数组数组?或 powershell 中的数组列表?
How do I make an array of arrays? or list of arrays in powershell?
$Colors = @{
1 = 'White'
2 = 'Black'
3 = 'DarkBlue'
4 = 'DarkGreen'
5 = 'DarkCyan'
6 = 'DarkRed'
7 = 'DarkMagenta'
8 = 'DarkYellow'
9 = 'Gray'
10 = 'DarkGray'
11 = 'Blue'
12 = 'Green'
13 = 'Cyan'
14 = 'Red'
15 = 'Magenta'
16 = 'Yellow'
}
$pixels = 1,2,3,4,5,6
foreach ($pixel in $pixels)
{
Write-Host " " -NoNewline -BackgroundColor $Colors.$pixel
}
我上面有这段代码,我试图用它在 powershell 中绘制像素化图像 window。现在它被设置为只绘制那一行像素。我想知道如何制作一个列表列表、数组数组或数组列表,例如,我可以使用它们来绘制 20x20 像素大小的图像。我熟悉如何在 python 中执行此操作,但不熟悉 powershell,如下所示。多维数组之类的东西。
$pixels = [(1,2,3,4,5,6)],
[(6,5,4,3,2,1)]
foreach ($pixel in $pixels)
{
Write-Host " " -NoNewline -BackgroundColor $Colors.$pixel
}
经过更多研究,我发现类似下面的内容可能有用,但我不知道如何正确地遍历它
System.Collections.ArrayList]$pixels = @()
$pixels = @(@(1,2,3),@(4,5,6),@(7,8,9))
foreach ($pixel in $pixels){ Write-Host " " -NoNewline -BackgroundColor $Colors.$pixel; Start-Sleep -m 50 }
您仍然可以创建一个 multi-dimensional 数组,但当然您仍然必须循环遍历每个单独的数组,并对每个数组中的每个元素再次循环。
我认为您只是缺少 inner-loop 和代码中的新行:
$Colors = @{
1 = 'White'
2 = 'Black'
3 = 'DarkBlue'
4 = 'DarkGreen'
5 = 'DarkCyan'
6 = 'DarkRed'
7 = 'DarkMagenta'
8 = 'DarkYellow'
9 = 'Gray'
10 = 'DarkGray'
11 = 'Blue'
12 = 'Green'
13 = 'Cyan'
14 = 'Red'
15 = 'Magenta'
16 = 'Yellow'
}
$arrays = @(1,2,3,4,5,6), @(6,5,4,3,2,1)
foreach ($array in $arrays)
{
foreach ($position in $array) {
Write-Host " " -NoNewline -BackgroundColor $Colors[$position]
}
Write-Host ""
}
$Colors = @{
1 = 'White'
2 = 'Black'
3 = 'DarkBlue'
4 = 'DarkGreen'
5 = 'DarkCyan'
6 = 'DarkRed'
7 = 'DarkMagenta'
8 = 'DarkYellow'
9 = 'Gray'
10 = 'DarkGray'
11 = 'Blue'
12 = 'Green'
13 = 'Cyan'
14 = 'Red'
15 = 'Magenta'
16 = 'Yellow'
}
$pixels = 1,2,3,4,5,6
foreach ($pixel in $pixels)
{
Write-Host " " -NoNewline -BackgroundColor $Colors.$pixel
}
我上面有这段代码,我试图用它在 powershell 中绘制像素化图像 window。现在它被设置为只绘制那一行像素。我想知道如何制作一个列表列表、数组数组或数组列表,例如,我可以使用它们来绘制 20x20 像素大小的图像。我熟悉如何在 python 中执行此操作,但不熟悉 powershell,如下所示。多维数组之类的东西。
$pixels = [(1,2,3,4,5,6)],
[(6,5,4,3,2,1)]
foreach ($pixel in $pixels)
{
Write-Host " " -NoNewline -BackgroundColor $Colors.$pixel
}
经过更多研究,我发现类似下面的内容可能有用,但我不知道如何正确地遍历它
System.Collections.ArrayList]$pixels = @()
$pixels = @(@(1,2,3),@(4,5,6),@(7,8,9))
foreach ($pixel in $pixels){ Write-Host " " -NoNewline -BackgroundColor $Colors.$pixel; Start-Sleep -m 50 }
您仍然可以创建一个 multi-dimensional 数组,但当然您仍然必须循环遍历每个单独的数组,并对每个数组中的每个元素再次循环。
我认为您只是缺少 inner-loop 和代码中的新行:
$Colors = @{
1 = 'White'
2 = 'Black'
3 = 'DarkBlue'
4 = 'DarkGreen'
5 = 'DarkCyan'
6 = 'DarkRed'
7 = 'DarkMagenta'
8 = 'DarkYellow'
9 = 'Gray'
10 = 'DarkGray'
11 = 'Blue'
12 = 'Green'
13 = 'Cyan'
14 = 'Red'
15 = 'Magenta'
16 = 'Yellow'
}
$arrays = @(1,2,3,4,5,6), @(6,5,4,3,2,1)
foreach ($array in $arrays)
{
foreach ($position in $array) {
Write-Host " " -NoNewline -BackgroundColor $Colors[$position]
}
Write-Host ""
}