包含重复项的数组

Array with repeated items

我正在寻找要实现的逻辑。我有两个数组,一个数组有 $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

如您的问题那样产生所需的输出(一个由 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 ', ' 包裹在 outer foreach 循环。


至于你尝试了什么:

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

  • 你不需要 @(...) 数组文字。

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

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

预期输出重复 $a,同时从 1..100 稳步递增,
所以嵌套循环不是实现这一目标的方法。

迭代 $b 并计算 $a 中的索引,并用 $a 的 length/count 的模数除法(并且由于基于零的索引而减去一)就可以了。

$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 ', '