0

how can i multiply each value by 10 in an array? the given array is

[int]$arr = 0..1000

now the way i have tried to solve it is:

foreach($valuex10 in $arr)
{
      $valuex10 = $valuex10 * 10
}

I think that should work, but it does not, i get the error "cant convert to System.Int32" if i try to give every variable the [int] (tag?) the ISE tells me it cant process it because there is no variable to start with. So what is wrong?

creating a new array is not allowed.

2
  • Remove [int] or use the correct [int[]] and use the index notation $arr[$i] = $arr[$i] * 10 inside a normal for loop on $i. Commented Jun 11, 2017 at 16:30
  • gonna try that after a smokebreak, could you tell me where i can read up on those notations? Commented Jun 11, 2017 at 16:44

2 Answers 2

1

You can use this:

$arr = 0..1000
for($i = 0; $i -lt $arr.Length; $i++){
    $arr[$i] = $arr[$i] * 10
}
Sign up to request clarification or add additional context in comments.

5 Comments

hey k7s5a --> our teacher disallowed using a new array, shouldve mentioned that in the Post
Add this fact to your question and try to understand what the code does!
$arr = 0..1000 (thats the array, got that) for($i = 0; $i -lt $arr.Length; $i++) (thats the for loop, with the runtimevariable (?) being 0, the condition being if runtimevariable less than $arr.length (but why array.length?), and the action of incrementing +1 { $arr[$i] = $arr[$i] * 10 (thats the codeblock, which defines $arr (and every element in it as element * 10?) }
($i -lt $arr.Length) -> That´s the loop condition. $arr.Length -> Is the length of the array. In your case 1000 cause you added 1000 items. $i = 0 -> Is the current loop index. $i++ -> increment the index after each round. $arr[$i] -> is the item of the current round index. Try this: $arr[0] and this $arr[$arr.Length-1]
Sorry, I think i got my wires crossed. You mean to tell me that its not possible doing a for loop on a Null-Array?
1

A functional approach is illustrated below, where the array expressed with 0..1000 is piped into the ForEach-Object command (aliased with %), which invokes a script block (the expression that follows the command name, starting with { and ending after matching }) for each element in the array:

0..1000 | % { $_ * 10 }

Evaluating the above expression yields an array where the first element is 10 times larger than the first element of input array (0..1000), the second element is 10 times larger than the second element of the [1..1000] array, and so on...

By the way, script blocks are "first class" objects in Powershell, you can assign them and use them like any other.

You can for instance reuse a script block:

$MultiplyByTen = { $_ * 10 }
1..1000 | % $MultiplyByTen

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.