Arrays 如何在powershell中创建置换数组?

Arrays 如何在powershell中创建置换数组?,arrays,powershell,permutation,Arrays,Powershell,Permutation,我有一个关于排列的问题。 例如,我有下表: 我想用这张桌子做排列。第1列中有两个选项,第2列和第3列中有三个选项。输出置换数组应如下所示: 有18个选项可将ITERM排列在1-3列中。问题是如何使用powershell创建脚本以获取这样的数组?如何将该数组输出到excel?您可以使用嵌套循环轻松完成此操作: $(foreach ($a in 'Single Load','Uniform distributed load') { foreach ($b in 'MAT1','MAT2','

我有一个关于排列的问题。 例如,我有下表:

我想用这张桌子做排列。第1列中有两个选项,第2列和第3列中有三个选项。输出置换数组应如下所示:


有18个选项可将ITERM排列在1-3列中。问题是如何使用powershell创建脚本以获取这样的数组?如何将该数组输出到excel?

您可以使用嵌套循环轻松完成此操作:

$(foreach ($a in 'Single Load','Uniform distributed load') {
  foreach ($b in 'MAT1','MAT2','MAT3') {
    foreach ($c in 'Country A','Country B','Country C') {
      New-Object -Prop @{Column1=$a;Column2=$b;Column3=$c}
    }
  }
}) | Export-Csv foo.csv -NoTypeInformation

然后可以通过Excel打开CSV文件。

您可以通过嵌套循环轻松完成此操作:

$(foreach ($a in 'Single Load','Uniform distributed load') {
  foreach ($b in 'MAT1','MAT2','MAT3') {
    foreach ($c in 'Country A','Country B','Country C') {
      New-Object -Prop @{Column1=$a;Column2=$b;Column3=$c}
    }
  }
}) | Export-Csv foo.csv -NoTypeInformation

然后,可以用Excel打开CSV文件。

好的,从Joey的优秀答案分支出来,要遍历具有已知列的数组,可以执行以下操作(PowerShell v3或更高版本,可以在旧版本中执行,但会变得更复杂)

这将生成阵列:

Column 1                 Column 2 Column 3 
--------                 -------- -------- 
Single load              MAT1     Country A
Single load              MAT1     Country B
Single load              MAT1     Country C
Single load              MAT2     Country A
Single load              MAT2     Country B
Single load              MAT2     Country C
Single load              MAT3     Country A
Single load              MAT3     Country B
Single load              MAT3     Country C
Uniform distributed load MAT1     Country A
Uniform distributed load MAT1     Country B
Uniform distributed load MAT1     Country C
Uniform distributed load MAT2     Country A
Uniform distributed load MAT2     Country B
Uniform distributed load MAT2     Country C
Uniform distributed load MAT3     Country A
Uniform distributed load MAT3     Country B
Uniform distributed load MAT3     Country C
要将其导入Excel,您可以按照Joey的建议导出到CSV文件,也可以让PowerShell打开Excel并直接将数据粘贴到其中

#Copy array to clipboard as a tab delimited CSV
$AllIterations | ConvertTo-CSV -Delimiter "`t" -NoTypeInfo | Select -Skip 1 | Clip

#Open Excel, create a blank workbook, and paste the data in at cell A1
$XL = New-Object -ComObject Excel.Application
$WB = $XL.Workbooks.Add()
$XL.Visible = $true
$XL.ActiveSheet.Cells.Item(1).PasteSpecial()

好的,从Joey的优秀答案出发,要遍历具有已知列的数组,可以执行以下操作(PowerShell v3或更高版本,可以在旧版本中执行,但会变得更复杂)

这将生成阵列:

Column 1                 Column 2 Column 3 
--------                 -------- -------- 
Single load              MAT1     Country A
Single load              MAT1     Country B
Single load              MAT1     Country C
Single load              MAT2     Country A
Single load              MAT2     Country B
Single load              MAT2     Country C
Single load              MAT3     Country A
Single load              MAT3     Country B
Single load              MAT3     Country C
Uniform distributed load MAT1     Country A
Uniform distributed load MAT1     Country B
Uniform distributed load MAT1     Country C
Uniform distributed load MAT2     Country A
Uniform distributed load MAT2     Country B
Uniform distributed load MAT2     Country C
Uniform distributed load MAT3     Country A
Uniform distributed load MAT3     Country B
Uniform distributed load MAT3     Country C
要将其导入Excel,您可以按照Joey的建议导出到CSV文件,也可以让PowerShell打开Excel并直接将数据粘贴到其中

#Copy array to clipboard as a tab delimited CSV
$AllIterations | ConvertTo-CSV -Delimiter "`t" -NoTypeInfo | Select -Skip 1 | Clip

#Open Excel, create a blank workbook, and paste the data in at cell A1
$XL = New-Object -ComObject Excel.Application
$WB = $XL.Workbooks.Add()
$XL.Visible = $true
$XL.ActiveSheet.Cells.Item(1).PasteSpecial()

这是一个对象数组(有列标题吗?)还是一个数组数组(没有列标题)?@TheMadTechnician,是的,我忘记放标题了。第1列、第2列和第3列。这是一个对象数组(有列标题吗?)还是一个数组数组(没有列标题)?@TheMadTechnician,是的,我忘记放标题了。第1列、第2列和第3列。这是一个很好的方法。然而,我想我需要一个脚本,可以做排列自动。如果我有很多数据,我需要花很长时间才能输入所有的名字。你有没有其他方法可以解决大量的数据@这很好,您只需执行
ForEach($array.Column 1'| Where{$\u})中的a(
等等。如果你不向<>代码> ,它也会迭代空白值。@ MeTeAdTe技师:只是考虑如何为任意数量的列来解决这个问题,这有点棘手。我认为我能想到的最好的方法是为
iex
生成代码。在不计算所有细节的情况下,我认为一个为嵌套目的而调用自身的函数与
$array.psobject.properties.name
相结合,应该允许您在任何数组中进行迭代,而不管列数是多少。如果这是我想要的东西,我可以试着为它编写代码。这是一个很好的方法。然而,我想我需要一个脚本,可以做排列自动。如果我有很多数据,我需要花很长时间才能输入所有的名字。你有没有其他方法可以解决大量的数据@这很好,您只需执行
ForEach($array.Column 1'| Where{$\u})中的a(
等等。如果你不向<>代码> ,它也会迭代空白值。@ MeTeAdTe技师:只是考虑如何为任意数量的列来解决这个问题,这有点棘手。我认为我能想到的最好的方法是为
iex
生成代码。在不计算所有细节的情况下,我认为一个为嵌套目的而调用自身的函数与
$array.psobject.properties.name
相结合,应该允许您在任何数组中进行迭代,而不管列数是多少。如果这是我想要的东西,我可以试着为它编写代码。