Powershell 重复项数组

Powershell 重复项数组,powershell,nested-loops,Powershell,Nested Loops,我正在寻找逻辑来实现。我有两个数组一个数组有$a=@(a1、b1、c1、d1、e1)和$b=@(1..100) 输出为:- 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

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

输出为:-

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个逗号分隔的重复名称-值对列表组成的数组,序列号稳步增加):

这将产生:

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
    相同-如果指定字符串作为范围端点,它们将强制为
    [int]

    • 请注意,PowerShell Core现在还允许您创建角色范围;e、 例如,
      'a'..'z'
      ;如果您希望使用恰好是数字的字符,请使用
      [char]
      强制转换以防止强制转换为
      [int]
      ;e、 g,
      [char]“1”。。[char]“3”

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

迭代
$b
并将索引计算为$a,其模数除以$a的长度/计数(由于基于零的索引,将一减法)

样本输出:

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
为了不使用重型管道,a-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 ', '

“很高兴听到这个答案很有帮助,”克兰蒂;我的荣幸。
$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
$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 ', '