question

jaime29-7516 avatar image
0 Votes"
jaime29-7516 asked AndreasBaumgarten commented

Powershell - 2 arrays

How will I write a code with 2 arrays where 1st array has 50 items and 2nd array has 7 items

Condition: for each item in Array 1 will assign 1 item from array 2, then 2nd item in array 1 and 2nd item in array 2..... and so on,

when 7th item in array 2, array 2 will reset to 1st item for item 8th in array 1.

Sample output

a = 1a
b = 2a
c = 3a
d = 4a
e = 5a
f = 6a
g = 7a
h = 1a
i = 2a
j = 3a
k = 4a
l = 5a
.........

Currently my 2nd array loop isn't working at all :(

windows-server-powershell
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @jaime29-7516 ,

Did an answer work for you? Are there any additional questions to this topic?

If you found the answer helpful, it would be great if you please mark it "Accept as answer". This will help others to find answers in Q&A

----------
Regards
Andreas Baumgarten

0 Votes 0 ·
MotoX80 avatar image
0 Votes"
MotoX80 answered
  $Array1 = @('a','b','c','d','e','f','g','h','i','j','k','l','m')
  $Array2 = @('1a','2a','3a','4a')
     
  $A2Idx = 0                          # Point to first entry in the second array 
  Foreach ($a1 in $Array1) {
      "{0} = {1}" -f $a1, $Array2[$A2Idx]
      $A2Idx++                        # Point to the next entry in the second array 
     if ($A2Idx -eq $Array2.count) {  # At end? 
         $A2Idx = 0                   # Point back to first entry in the second array 
     }      
  }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered AndreasBaumgarten edited

Hi @jaime29-7516 ,

maybe this helps:

 $a = @('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o')
 $b = @('1a', '2a', '3a', '4a', '5a', '6a', '7a')   
 $bI = 0
 $result = @()
 Foreach ($item in $a) {
     $result += $item, $b[$bI] -join "="
     $bI++
     if ($bI -eq $b.count) { 
         $bI = 0
     }      
 }
 $result


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LimitlessTechnology-2700 avatar image
0 Votes"
LimitlessTechnology-2700 answered RichMatheisen-8856 commented

Hi there,


Use the + operator to add to arrays together:

PS C:\> $a = 2,3,4

PS C:\> $b = 5,6,7

PS C:\> $c = $a + $b

PS C:\> $c

Hope this Answers all your queries , if not please do repost back .
If an Answer is helpful, please click "Accept Answer" and upvote it : )

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Did you read the question? Did you understand the question? Given that answer I'd say not.

2 Votes 2 ·