重复项数组

时间:2019-03-27 20:08:51

标签: powershell nested-loops

我正在寻找实现的逻辑。我有两个数组,一个数组有$a=@(a1,b1,c1,d1,e1)$b=@(1..100)

$b=@('1'..'100')
$a=@('a1','b1','c1','d1','e1')
foreach($k in $a){
  foreach($j in $b){
     $j = $k
     Write-Host "The variable is :"$j" and the result is: "$k
  }
}

输出为:-

The variable is : 1 and the result is: 1
The variable is : 1 and the result is: 1
The variable is : 1 and the result is: 1
The variable is : 1 and the result is: 1
The variable is : 1 and the result is: 1
The variable is : 2 and the result is: 2
The variable is : 2 and the result is: 2
The variable is : 2 and the result is: 2
The variable is : 2 and the result is: 2
The variable is : 2 and the result is: 2
The variable is : 3 and the result is: 3
The variable is : 3 and the result is: 3
The variable is : 3 and the result is: 3
The variable is : 3 and the result is: 3
The variable is : 3 and the result is: 3
The variable is : 4 and the result is: 4
The variable is : 4 and the result is: 4
The variable is : 4 and the result is: 4
The variable is : 4 and the result is: 4
The variable is : 4 and the result is: 4
The variable is : 5 and the result is: 5
The variable is : 5 and the result is: 5
The variable is : 5 and the result is: 5
The variable is : 5 and the result is: 5
The variable is : 5 and the result is: 5
The variable is : 6 and the result is: 6
The variable is : 6 and the result is: 6
The variable is : 6 and the result is: 6
The variable is : 6 and the result is: 6
The variable is : 6 and the result is: 6
The variable is : 7 and the result is: 7

实际上输出应如下:-

a1=1, b1=2, c1=3, d1=4, e1=5 again a1=6,b1=7,c1=8,d1=9, e1=10....a1=96,b1=97,c1=98,d1=99,e1=100

2 个答案:

答案 0 :(得分:1)

要像您的问题中那样产生所需的输出(一个由20个逗号分隔的列表组成的列表,这些列表由重复的名称/值对组成,并且序列号不断增加):

$a = 'a1', 'b1', 'c1', 'd1', 'e1'
$i = 0 # sequence number
foreach ($pass in 1..20) {
  $(foreach ($el in $a) {
    ++$i
    "$el=$i"
  }) -join ', '
}

这将产生:

a1=1, b1=2, c1=3, d1=4, e1=5
a1=6, b1=7, c1=8, d1=9, e1=10
a1=11, b1=12, c1=13, d1=14, e1=15
a1=16, b1=17, c1=18, d1=19, e1=20
a1=21, b1=22, c1=23, d1=24, e1=25
a1=26, b1=27, c1=28, d1=29, e1=30
a1=31, b1=32, c1=33, d1=34, e1=35
a1=36, b1=37, c1=38, d1=39, e1=40
a1=41, b1=42, c1=43, d1=44, e1=45
a1=46, b1=47, c1=48, d1=49, e1=50
a1=51, b1=52, c1=53, d1=54, e1=55
a1=56, b1=57, c1=58, d1=59, e1=60
a1=61, b1=62, c1=63, d1=64, e1=65
a1=66, b1=67, c1=68, d1=69, e1=70
a1=71, b1=72, c1=73, d1=74, e1=75
a1=76, b1=77, c1=78, d1=79, e1=80
a1=81, b1=82, c1=83, d1=84, e1=85
a1=86, b1=87, c1=88, d1=89, e1=90
a1=91, b1=92, c1=93, d1=94, e1=95
a1=96, b1=97, c1=98, d1=99, e1=100

请注意使用可扩展字符串"$el=$i"来生成每个名称/值对。

在每遍中,$(...)围绕内部foreach循环收集对,-join然后转换成逗号分隔的列表(单个字符串); foreach外部循环的输出是一个包含20个列表的数组。

如果您想要一个包含所有条目的单个列表,请将$(...) -join ', '包裹在外部 foreach循环周围。


关于您尝试过的事情:

  • $j = $k不输出(扩展的)字符串,它是一个变量分配:它将变量$k的值分配给变量$j

  • 您不需要在数组文字周围使用@(...)

  • '1'..'100'1..100相同-如果您将 strings 指定为范围端点,它们将被强制为[int]值。 / p>

    • 请注意,PowerShell Core 现在还允许您创建 character 范围;例如'a'..'z';在极少数情况下,您想使用恰好是数字的字符,请使用[char]强制转换以防止其强制转换为[int];例如[char] '1'.. [char] '3'

答案 1 :(得分:1)

预期输出重复$ a,同时从1..100稳定递增,
因此嵌套循环不是实现该目标的方法。

迭代$b并用系数除以$ a的长度/计数(并由于从零开始的索引减去1),将索引计算为$ a即可。

$b=@(1..100)
$a=@('a1','b1','c1','d1','e1')
($b|ForEach-Object {
    "{0}={1}" -f $a[($_ % $a.count)-1],$_
}) -join ', '

示例输出:

a1=1, b1=2, c1=3, d1=4, e1=5, a1=6, b1=7, c1=8, d1=9, e1=10, a1=11, b1=12, c1=13, d1=14, e1=15, a1=16, b1=17, c1=18, d1=19, e1=20, a1=21, b1=22, c1=23, d1=24, e1=25, a1=26, b1=27, c1=28, d1=29, e1=30, a1=31, b1=32, c1=33, d1=34, e1=35, a1=36, b1=37, c1=38, d1=39, e1=40, a1=41, b1=42, c1=43, d1=44, e1=45, a1=46, b1=47, c1=48, d1=49, e1=50, a1=51, b1=52, c1=53, d1=54, e1=55, a1=56, b1=57, c1=58, d1=59, e1=60, a1=61, b1=62, c1=63, d1=64, e1=65, a1=66, b1=67, c1=68, d1=69, e1=70, a1=71, b1=72, c1=73, d1=74, e1=75, a1=76, b1=77, c1=78, d1=79, e1=80, a1=81, b1=82, c1=83, d1=84, e1=85, a1=86, b1=87, c1=88, d1=89, e1=90, a1=91, b1=92, c1=93, d1=94, e1=95, a1=96, b1=97, c1=98, d1=99, e1=100

为了不使用笨重的管道,for应当更快

$a=@('a1','b1','c1','d1','e1')
$output = for ($b=1;$b -le 100;$b++){"{0}={1}" -f $a[($b % $a.count)-1],$b}
$output -join ', '