secondplace
secondplace

Reputation: 578

powershell execute math on array elements

I'm looking to perform math on two arrays but i'm always ending up executing on the arrays itself and not it elements. how do i call each element from both arrays and perform math on them one by one?

$array1 = (1,2,3)
$array2 = (1,2,3)
$do | % {$array1 + $array2}

this adds the arrays together as in:

1
2
3
1
2
3

but the result i am looking for is the following:

2
4
6

how do i have to go about this?

Upvotes: 0

Views: 979

Answers (1)

Paxz
Paxz

Reputation: 3036

One way would be to use for like this:

$array1 = (1,2,3)
$array2 = (1,2,3)

for ($i = 0; $i -lt $array1.Length; $i++){
  $array1[$i] + $array2[$i]
}

Output:

2
4
6

Upvotes: 2

Related Questions