I have to write a script for my class with an output like this:
--ENTER--INTRUSIONS-----
Mon 11
Tue 2
Wed 5
Thr 7
Fri 13
------INTRUSIONS--------
Mon 11
Tue 2
Wed 5
Thr 7
Fri 13
Total 38
Average 7.6
------------------------
Mon had the most intrusions at 11
Tue had the least intrusions at 2
------------------------
I can get all the information but I can’t figure out how to get the day to show for the last two lines.
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.
If you have related data you should save them together as related data.
I’d do it this way:
$Data = @'
Day,AttackCount
Mon,
Tue,
Wed,
Thr,
Fri,
'@ |
ConvertFrom-Csv
Clear-Host
foreach ($item in $Data) {
[INT]$item.AttackCount = Read-Host -Prompt "Enter amount of attacks for '$($item.Day)'"
}
$Result = $Data | Measure-Object -Property AttackCount -AllStats
$SortedData = $Data | Sort-Object -Property AttackCount
Clear-Host
" Total amount of attacks: '$($Result.Sum)'"
"Average amount of attacks: '$($Result.Average)'"
"Minimum amount of attacks: '$($Result.Minimum)' at '$($SortedData[0].Day)'"
"Maximum amount of attacks: '$($Result.Maximum)' at '$($SortedData[-1].Day)'"
Of course - as always - there’s a lot of room for improvements.
When you post error messages you should always post them completely.
And what have you tried to solve this issue? You should ALWAYS read the help for the cmdlets you’re about to use. Read it complete including the examples to learn how to use them.
You may compare the two help versions :
Well … I would have simply changed -AllStats to -Sum -Average -Maximum -Minimum as that would provide the needed results. But it’s a great idea as well to have the new version of Powershell and maybe benefit from new features.