Answered by:
Need help on nested loop

Question
-
Hi Team,
I am creating script with nested loop. Would someone please provide me output for below script as a1,b2,c3.
I am using normal nested loop, it gives me below answer:
a1
a2
a3
b1
b2
b3
c1
c2
c3
$alpha = "a","b","c"
$num = 1,2,3
foreach($a in $alpha)
{
foreach($n in $num)
{
write-host $a$n
}
}Helping to get output as a1,b2,c3 would be really appreciated.
Regards
Jatinder Pal Singh (JPS)
JPS
Thursday, January 23, 2020 7:58 PM
Answers
-
Simple. First turn on brain, second ask correct question and third wait for compute results.
$alpha = 'a','b','c' $num = 1,2,3 for($i = 0;$i -lt 3;$i++){ Write-Host $alpha[$i]$num[$i] }
\_(ツ)_/
- Marked as answer by jps0319 Saturday, January 25, 2020 9:45 AM
Friday, January 24, 2020 3:30 PM
All replies
-
Homework?
$alpha = 'a','b','c' $num = 1,2,3 $arr = foreach($a in $alpha){ foreach($n in $num){ "$a$n" } } $arr -join ','
\_(ツ)_/
Thursday, January 23, 2020 8:09 PM -
Hi jrv,
I think you didn't get my point. May be I was not able to describe it properly. I need the output as below:
a1
b2
c3
Let me explain. The script I am using where $a is getting in loop 3 time in its sub/child loop. I just want $a to be run once in it sub/child loop so that it does not give output a2,a3. I need similar output with $b and $c i.e. b2 and c3.
Please let me know if you need any further explanation.
JPS
Friday, January 24, 2020 12:17 PM -
Simple. First turn on brain, second ask correct question and third wait for compute results.
$alpha = 'a','b','c' $num = 1,2,3 for($i = 0;$i -lt 3;$i++){ Write-Host $alpha[$i]$num[$i] }
\_(ツ)_/
- Marked as answer by jps0319 Saturday, January 25, 2020 9:45 AM
Friday, January 24, 2020 3:30 PM -
Thanks jrv, it worked but I changed it little. I asked same question in different way.
$alpha = 'a','b','c'
$num = 1,2,3
for($i = 0;$i -lt 3;$i++){
Write-Host ($alpha[$i]+$num[$i])
}JPS
Friday, January 24, 2020 7:14 PM