Search code examples
powershellnested-loops

Array with repeated items


i am looking for logic to implement. I am having two arrays one array is having $a=@(a1,b1,c1,d1,e1) and $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 output is:-

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

Actually the output should be the below:-

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

Solution

  • To produce the desired output as in your question (an array of 20 comma-separated lists of repeating name-value pairs with a steadily increasing sequence number):

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

    This yields:

    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
    

    Note the use of expandable string "$el=$i" to produce each name-value pair.

    In each pass, the pairs are collected by the $(...) around the inner foreach loop, which -join then converts into a comma-separated list (a single string); the output from the outer foreach loop is an array that contains the 20 lists.

    If you want a single list with all entries instead, wrap the $(...) -join ', ' around the outer foreach loop instead.


    As for what you tried:

    • $j = $k doesn't output an (expanded) string, it is a variable assingment: it assigns the value of variable $k to variable $j.

    • You don't need @(...) around array literals.

    • '1'..'100' is the same as 1..100 - if you specify strings as the the range endpoints, they are coerced to [int] values.

      • Note that PowerShell Core now also lets you create character ranges; e.g., 'a'..'z'; in the rare event that you want to use a character that happens to be a digit, use a [char] cast to prevent its coercion to [int]; e.g, [char] '1'.. [char] '3'.