Reputation: 3832
If I have 2 nested loops, how do I refer to current item in outer loop inside inner loop? Is it possible?
$arrayA = @(1..10)
$arrayB = @(11.20)
$arrayA.ForEach({$_; $arrayb.Where({$_ -eq $_})})
Upvotes: 1
Views: 669
Reputation: 19684
Instead of using the array methods, use the cmdlets:
$arrayA | ForEach-Object -PipelineVariable item {
$arrayB.Where{$item -eq $_}
}
# shortened
$arrayA | % -pv item { $_; $arrayB.Where{$item -eq $_} }
-PipelineVariable
was introduced in v4 (which you have based off your use of array methods ForEach
and Where
).
Although a better solution:
$arrayA.Where{$_ -in $arrayB}
Upvotes: 3
Reputation: 3918
Here is a short example of how you can access the current value of the element of the first array within the loop of the second array:
$arrayA = @(1..10)
$arrayB = @(1..10)
$arrayA | foreach {
$tempA = $_
$arrayB | foreach {
"$tempA - $_"
}
}
Just run the code to get an idea.
Upvotes: 0