mawazo
June 20, 2020, 4:38am
1
Hi all,
i have an array
$number = 50,10,25,60,84
i want to subtract 50 from 500 then update that value and continue down the list. something like this
500-50 = 450
450-10 = 440
440-25 = 415
415-60 = 355
355-84 = 271
i can do it manually
$amount = 500
$number[0]= $amount - $number[0]
$number[1]= $number[0] - $number[1]
$number[2]= $number[1] - $number[2]
$number[3]= $number[2] - $number[3]
$number[4]= $number[3] - $number[4]
$number
PS C:\Users\Administrator> $number
450
440
415
355
271
Please may i ask for some guidance as to how i can do this using for statement?
i keep failing, below is what i tried but only works for the first value.
for($i=0; $i -lt $number.Count; $i++){
$amount - $number[$i]
}
Thanks
Olaf
June 20, 2020, 5:01am
2
Jam, welcome to Powershell.org . Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did! .
When you post code, error messages, sample data or console output format it as code, please.
In the “Text ” view you can use the code tags “PRE ”, in the “Visual ” view you can use the format template “Preformatted ”. You can go back edit your post and fix the formatting - you don’t have to create a new one.
Thanks in advance.
$numberList = 50,10,25,60,84
$Amount = 500
foreach ($Number in $numberList) {
$Amount -= $Number
}
$Amount
mawazo
June 20, 2020, 6:26am
3
Man i didn’t know you can subtract like that. i spent hours on this lol
Thank you Olaf! Much appreciated. i’ve edited the post.
I got my list \o/
$numberList = 50,10,25,60,84
$Amount = 500
$UpdatedList = @()
foreach ($Number in $numberList) {
$UpdatedList += [pscustomobject]@{'Amount' = $Amount-=$Number
'Numbers'= $Number}
}
$UpdatedList
Olaf
June 20, 2020, 7:10am
4
$numberList = 50, 10, 25, 60, 84
$Amount = 500
$UpdatedList = @()
$UpdatedList =
foreach ($Number in $numberList) {
[pscustomobject]@{
'Amount' = $Amount -= $Number
'Numbers' = $Number
}
}
$UpdatedList