Text processing/1: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) m (→{{header|Sidef}}: Fix link: Perl 6 --> Raku) |
(PowerShell added) |
||
Line 2,895: | Line 2,895: | ||
end text1;</lang> |
end text1;</lang> |
||
=={{header|PowerShell}}== |
|||
<lang PowerShell>$file = '.\readings.txt' |
|||
$lines = Get-Content $file # $args[0] |
|||
$valid = $true |
|||
$startDate = $currStart = $endDate = '' |
|||
$startHour = $endHour = $currHour = $max = $currMax = $total = $readings = 0 |
|||
$task = @() |
|||
foreach ($var in $lines) { |
|||
$date, $rest = [regex]::Split($var,'\s+') |
|||
$reject = $accept = $sum = $cnt = 0 |
|||
while ($rest) { |
|||
$cnt += 1 |
|||
[Double]$val, [Double]$flag, $rest = $rest |
|||
if (0 -lt $flag) { |
|||
$currMax = 0 |
|||
$sum += $val |
|||
$accept += 1 |
|||
} else { |
|||
if (0 -eq $currMax) { |
|||
$currStart = $date |
|||
$currHour = $cnt |
|||
} |
|||
$currMax += 1 |
|||
$reject += 1 |
|||
if ($max -lt $currMax) { |
|||
$startDate, $endDate = $currStart, $date |
|||
$startHour, $endHour = $currHour, $cnt |
|||
$max = $currMax |
|||
} |
|||
} |
|||
} |
|||
$readings += $accept |
|||
$total += $sum |
|||
$average = if (0 -lt $accept) {$sum/$accept} else {0} |
|||
$task += [PSCustomObject]@{ |
|||
'Line' = $date |
|||
'Reject' = $reject |
|||
'Accept' = $accept |
|||
'Sum' = $sum.ToString("N") |
|||
'Average' = $average.ToString("N3") |
|||
} |
|||
$valid = 0 -eq $reject |
|||
} |
|||
$task | Select -Last 3 |
|||
$average = $total/$readings |
|||
"File(s) = $file" |
|||
"Total = {0}" -f $total.ToString("N") |
|||
"Readings = $readings" |
|||
"Average = {0}" -f $average.ToString("N3") |
|||
"" |
|||
"Maximum run(s) of $max consecutive false readings" |
|||
if (0 -lt $max) { |
|||
"Consecutive false readings starts at line starting with date $startDate at hour {0:0#}:00." -f $startHour |
|||
"Consecutive false readings ends at line starting with date $endDate at hour {0:0#}:00." -f $endHour |
|||
}</lang> |
|||
<pre>Line : 2004-12-29 |
|||
Reject : 1 |
|||
Accept : 23 |
|||
Sum : 56.30 |
|||
Average : 2.448 |
|||
Line : 2004-12-30 |
|||
Reject : 1 |
|||
Accept : 23 |
|||
Sum : 65.30 |
|||
Average : 2.839 |
|||
Line : 2004-12-31 |
|||
Reject : 1 |
|||
Accept : 23 |
|||
Sum : 47.30 |
|||
Average : 2.057 |
|||
File(s) = .\readings.txt |
|||
Total = 1,358,393.40 |
|||
Readings = 129403 |
|||
Average = 10.497 |
|||
Maximum run(s) of 589 consecutive false readings |
|||
Consecutive false readings starts at line starting with date 1993-02-09 at hour 02:00. |
|||
Consecutive false readings ends at line starting with date 1993-03-05 at hour 14:00.</pre> |
|||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |