0

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?

1
  • 1
    0..($array1.length-1)|%{$array1[$_]+$array2[$_]} Commented Jul 27, 2018 at 18:06

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.