C
C
CrazyKing2019-05-29 12:55:14
PowerShell
CrazyKing, 2019-05-29 12:55:14

How to correctly read an array in a Powershell loop?

Good day everyone!
There are arrays:

$target1 = ("D:\1", "D:\2","D:\3")
$target2 = ("C:\1", "C:\2", "C:\3")
$target3 = ($target1,$target2)

I'm trying to iterate over $target3 in a loop and select certain values ​​into variables
foreach($foreachTarget in  $target3)
        {    
            $src = $foreachTarget[1]
            $xdt = $foreachTarget[0]
            ECHO $src $xdt
        }

as a result I get:
D:\2 D:\1
C:\2 C:\1

And I need it to sort through them one by one, the result should be like this:
D:\1 C:\1
D:\2 С:\2
D:\3 С:\3

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sergey, 2019-05-29
@CrazyKing

# example from https://stackoverflow.com/questions/43122000/how-c...
#

$func = { param ($a, $b)  ('{0} {1}' -f $a , $b)}
[Linq.Enumerable]::Zip(@("D:\1", "D:\2","D:\3"), @("C:\1", "C:\2", "C:\3"), [Func[Object, Object, Object[]]]$func)

D:\1 C:\1
D:\2 C:\2
D:\3 C:\3

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question