I have an array of values (about 20 of them), ranging from about 9.5 to 0.001. I need to add the values together to get a number anywhere between 15 and 85 (Or get the nearest value possible)
Test data:
[9.912,7.414,5.512, 5.43,5.303,5.041,5.025,4.944,3.509, 3.3,3.191,3.076,2.914,2.753, 2.29,1.955,1.917,1.822,1.626,1.526,1.489,1.482,1.362,1.354,1.311,1.222,1.154,0.904,0.799, 0.79,0.657,0.643,0.618,0.615,0.592,0.564,0.484, 0.48,0.447,0.419,0.415,0.328,0.325,0.288, 0.26,0.251,0.248,0.23,0.223,0.221,0.154,0.129,0.128,0.126,0.103,0.102,0.099,0.092,0.077,0.071,0.066, 0.06,0.046,0.037,0.028,0.023,0.007,0.003,0.003,0.003,0.002,0.001,0.001,0.001,0.001]
Can anyone help me/is this even possible?
You could do it like that:
$arr = #(9.912,7.414,5.512, 5.43,5.303,5.041,5.025,4.944,3.509, 3.3,3.191,3.076,2.914,2.753, 2.29,1.955,1.917,1.822,1.626,1.526,1.489,1.482,1.362,1.354,1.311,1.222,1.154,0.904,0.799, 0.79,0.657,0.643,0.618,0.615,0.592,0.564,0.484, 0.48,0.447,0.419,0.415,0.328,0.325,0.288, 0.26,0.251,0.248,0.23,0.223,0.221,0.154,0.129,0.128,0.126,0.103,0.102,0.099,0.092,0.077,0.071,0.066, 0.06,0.046,0.037,0.028,0.023,0.007,0.003,0.003,0.003,0.002,0.001,0.001,0.001,0.001)
$output = 0
$arr | foreach {
if(($output + $_) -le 85){
$output += $_
}
}
$output
The above example will return a number close to but less than 85
This answer here: Powershell break a long array into a array of array with length of N in one line?
almost perfectly answers my requirements. What I would like to do is group my elements so that the first group has one extra element in it and all others have the first element repeated in each group.
Like this:
$bigList = ("Name","One","Two","Three","Four","Five","six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen")
$counter = [pscustomobject] #{ Value = 0 }
$groupSize = 5
$groups = $bigList | Group-Object -Property { [math]::Floor($counter.Value++ / $groupSize) }
With my groups ending up looking like this ...
Name, One, Two, Three, Four, Five
Name, Six, Seven, Eight, Nine, Ten
Name, Eleven, Twelve, Thirteen
How's this:
$bigList = ("Name","One","Two","Three","Four","Five","six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen")
$counter = [pscustomobject] #{ Value = 0 }
$groupSize = 5
$bigList[1..$biglist.count] |
Group-Object -Property { [math]::Floor($counter.Value++ / $groupSize) } |
foreach-object {
$_.Group.Insert(0,$biglist[0])
write-output $_
}
Output:
Count Name Group
----- ---- -----
5 0 {Name, One, Two, Three...}
5 1 {Name, six, Seven, Eight...}
3 2 {Name, Eleven, Twelve, Thirteen}
This works by stripping out the first element ("Name") and sending the rest of the array through the group-object cmdlet. The resulting objects are modified in the foreach-object scriptblock by inserting the value from the first element of the big list into the group property. The modified object is re-written to the pipeline.
Assuming you want these in an list of arrays, you'd need to modify the code slightly:
$arrayList = $bigList[1..$biglist.count] |
Group-Object -Property { [math]::Floor($counter.Value++ / $groupSize) } |
ForEach-Object {
$_.Group.Insert(0,$biglist[0])
,$_.group
}
$arraylist |
% {
write-host ( $_ -join "," )
}
Here, we deliberately output only the group property of the groupinfo object that comes from group-object. In addition, we use the , operator to force powershell to output an individual array. If this is not done, we end up with a single array that is just like $biglist, but with extra instances of 'Name' every 5 elements. The final chunk of code outputs the contents of each element of $arrayList, concatenating with commas to demonstrate that we do indeed have an array of arrays:
Name,One,Two,Three,Four,Five
Name,six,Seven,Eight,Nine,Ten
Name,Eleven,Twelve,Thirteen
If you would like a complete breakdown of the script and the results I have obtained so far, you can reference my previous post.
Import the items from the pipeline in to an array
I am trying to figure out how to get only the two fields I need from the array, so I can manipulate the data. Right now the array shows the entire line as the first element in the array.
$Date = (Get-Date -format "MM-dd-yyyy")
$DateTime = (Get-Date)
$Projects = Import-Csv c:\temp\pm_project.csv
#Show me the entire list of Projects
$Projects
$TS_Entries = Get-Content "c:\temp\timesheet\$Date.txt"
#Show me all the chosen entries in the text file
$TS_Entries
#Show me only the lines in the text file that match the regex statement
$TS_Entries = Select-String -Path "C:\temp\TimeSheet\$Date.txt" -Pattern '(?>FOAH|PRJ)\d{1,10}' -allmatches | Select-Object -expand matches | Select-Object -expand Value
$TS_Entries
$TS_Entries | group -NoElement
I cannot seem to utilize the count element in the array. I need to get a value of each value in Count and multiply them by 15. The only thing I can reference in the list below is Name
Count Name
----- ----
2 FOAH278
1 FOAH519
1 FOAH704
3 FOAH718
2 FOAH780
So i'm not sure i fully understand what you are asking but here goes:
So say you have a txtfile :
C:\temp\TimeSheet\11-06-2017.txt:1:11/06/2017 18:45:56 - This 15 minutes is dedicated to PROJECT 100 FOAH18
C:\temp\TimeSheet\11-06-2017.txt:2:11/06/2017 18:45:58 - This 15 minutes is dedicated to PROJECT 90 FOAH278
C:\temp\TimeSheet\11-06-2017.txt:3:11/06/2017 18:45:59 - This 15 minutes is dedicated to PROJECT 80 FOAH313
C:\temp\TimeSheet\11-06-2017.txt:4:11/06/2017 18:46:00 - This 15 minutes is dedicated to PROJECT 70 PRJ0031905
C:\temp\TimeSheet\11-06-2017.txt:5:11/06/2017 18:46:02 - This 15 minutes is dedicated to PROJECT 60 PRJ0031909
C:\temp\TimeSheet\11-06-2017.txt:6:11/06/2017 18:46:03 - This 15 minutes is dedicated to PROJECT 50 PRJ0032045
The following script uses .NET regex class
$test = gc "C:\temp\stack.txt"
$m = [regex]::Matches($test,'(?msi)((?<=Project )\d{0,}).*?(FOAH|PRJ)(\d{1,10})')
Example of regex: https://regex101.com/r/yhgl9v/1
foreach($match in $m){
write-host "ID:" $match.Groups[1].Value
write-host "Prefix:" $match.Groups[2].Value
write-host "Number:" $match.Groups[3].Value
""
}
This will give you:
ID: 100
Prefix: FOAH
Number: 18
ID: 90
Prefix: FOAH
Number: 278
ID: 80
Prefix: FOAH
Number: 313
ID: 70
Prefix: PRJ
Number: 0031905
ID: 60
Prefix: PRJ
Number: 0031909
ID: 50
Prefix: PRJ
Number: 0032045
Is it this you are looking for?
$test = gc "C:\temp\stack.txt"
$m = [regex]::Matches($test,'(?msi)((?<=Project )\d{0,}).*?(FOAH|PRJ)(\d{1,10})')
$arr=#()
foreach($match in $m){
$arr += [PSCustomObject]#{
id = $match.Groups[1].Value
prefix = $match.Groups[2].Value
number = $match.Groups[3].Value
}
}
$arr | Export-Csv C:\temp\stock.csv -NoTypeInformation -Delimiter ';'
So you create a customobject (hashtable) that works with a value-index system.
Once you've filled up your array, you can later on (outside the loop), export it into a csv-file.
Your CSV-file will contain:
"id";"prefix";"number"
"100";"FOAH";"18"
"90";"FOAH";"278"
....
Here is my code:
Get-Content 'hist4.txt' | ForEach-Object { $_.split(" ")[0]} |
ForEach-Object {
$rgbArray += $_
for( $i = 1; $i -le $rgbArray.length; $i++ ) {
$rgbA0=$rgbArray[$i]
$rgbA1=$rgbArray[$i + 1]
//compare A0 and A1...
}
}
The text file contents:
1 500
21 456
33 789
40 653
54 900
63 1000
101 1203
I want to load the text file. Get-Content 'hist4.txt'
It has two elements on each line. I need the first element. ForEach-Object { $_.split(" ")[0]}
I need to store the first element of each line into an array: $rgbArray += $_
then iterate over each element of the array and do stuff: for( $i = 1; $i -le $rgbArray.length; $i++ )
If I use $rgbArray = #(), then the entire list of elements from the split() command are all stored at $rgbArray[0]. This prevents me from looping over them because the array length is only 1, even though there are hundreds of values stored at the 0 location of the array.
If I use $rgbArray += then I do get the elements to loop. However, this causes each location of the array to be the same as the last location but plus the next character, and not the whole element from the split command.
For example: $rgbArray += $_ outputs
$rgbArray[0] = 1
$rgbArray[1] = 12
$rgbArray[2] = 121
$rgbArray[3] = 1213
$rgbArray[4] = 12133
$rgbArray[5] = 121334
$rgbArray = #($_) outputs
$rgbArray[0] = 1 21 33 40 54 63 ....
$rgbArray[1] =
$rgbArray[2] =
$rgbArray[3] =
$rgbArray[4] =
$rgbArray[5] =
where I need it to be :
$rgbArray[0] = 1
$rgbArray[1] = 21
$rgbArray[2] = 33
$rgbArray[3] = 40
$rgbArray[4] = 54
$rgbArray[5] = 63
and so on.
I would like to know if I am not creating the array correctly and how to get the first element of each line as a separate array element?
Later in the code I would like to be able to compare the first element of different lines in the text file to other first elements.
Start by assigning the output from the first two pipeline elements to the $rgbArray variable:
$rgbArray = Get-Content 'hist4.txt' | ForEach-Object { $_.Split(" ")[0] }
With that squared away, you can start working with the data:
for( $i = 0; $i -lt ($rgbArray.Count - 1); $i++ ) {
$current = $rgbArray[$i]
$next = $rgbArray[$i + 1]
# compare $current and $next here
}
Code:
$arr1 = "" | select blabla,blabla2
$arr2 = "" | select blabla3,blabla4
$arrtotal = #()
$arrtotal += $arr1
$arrtotal += $arr2
$arrtotal
Printout:
blabla blabla2
However, when attempting to print both cells individually (not one after the other but simply selecting in PS ISE and hitting F8):
$arrtotal[0]
blabla blabla2
$arrtotal[1]
blabl3 blabla4
EDITED:
I would have expected both array columns to be printed when printing $arrtotal. Not just one of them. Further more it's unclear to me why printing them individually works but one after the other i.e "$arrtotal[0];$arrtotal[1]" does not.
EDIT2:
This is my original code.
All it does is query Sparkpost's API in order to build a custom HTML report.
$test = (Invoke-WebRequest "https://api.sparkpost.com/api/v1/metrics/deliverability?metrics=count_injected,count_sent,count_bounce,count_accepted&from=2016-01-01T08:00&to=2016-04-25T08:00" -Headers #{"Authorization"="xxxxxxxxxxxxx";"Content-Type"= "application/json"}).content | ConvertFrom-Json
$fill1 = "" | select EmailsReceived,EmailsSent,EmailsBounced
$fill1.EmailsReceived = $test.results.count_injected
$fill1.EmailsSent = $test.results.count_accepted
$fill1.EmailsBounced = $test.results.count_bounce
$fill2 = "" | select DeliveredPrecentage,BouncesPrecentage
$fill2.DeliveredPrecentage = [math]::round($test.results.count_accepted/$test.results.count_injected*100,2)
$fill2.BouncesPrecentage = [math]::round(($test.results.count_bounce)/$test.results.count_accepted*100,2)
$arr = #()
$arr += , $fill1
$arr += , $fill2
My problem is that I cant simply convert $arr into an HTML file like I've done numerous times before.
$arr
EmailsReceived EmailsSent EmailsBounced
107 107 12
On the other hand
$arr | Format-List
EmailsReceived : 107 EmailsSent : 107 EmailsBounced : 12
DeliveredPrecentage : 100 BouncesPrecentage : 11.21
I'd like to make an HTML out of everything so I can send it via email later. How can I pipe it all?
Its because $arr1and $arr2are two different PSCustomObjects. You can print the whole arrayin a list using the Format-List cmdlet:
$arrtotal | Format-List
Output:
blabla :
blabla2 :
blabla :
blabla2 :
Answer to your edit:
This looks like a single record for me, try this:
$record = [PsCustomObject]#{
EmailsReceived = $test.results.count_injected
EmailsSent = $test.results.count_accepted
EmailsBounced = $test.results.count_bounce
DeliveredPrecentage = [math]::round($test.results.count_accepted/$test.results.count_injected*100,2)
BouncesPrecentage = [math]::round(($test.results.count_bounce)/$test.results.count_accepted*100,2)
}
$record | convertto-html | out-file file.html
Note: I also changed the logic to create the object to a more well known approach using a PsCustomObject typecast on a hashtable.