Text processing/1: Difference between revisions

m
(Added Wren)
m (→‎{{header|Wren}}: Minor tidy)
 
(10 intermediate revisions by 8 users not shown)
Line 34:
 
Structure your program to show statistics for each line of the file, (similar to the original Python, Perl, and AWK examples below), followed by summary statistics for the file. When showing example output just show a few line statistics and the full end summary.
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">V nodata = 0
V nodata_max = -1
[String] nodata_maxline
V tot_file = 0.0
V num_file = 0
 
:start:
L(line) File(:argv[1]).read().rtrim("\n").split("\n")
V tot_line = 0.0
V num_line = 0
 
V field = line.split("\t")
V date = field[0]
V data = field[(1..).step(2)].map(f -> Float(f))
V flags = field[(2..).step(2)].map(f -> Int(f))
 
L(datum, flag) zip(data, flags)
I flag < 1
nodata++
E
I nodata_max == nodata & nodata > 0
nodata_maxline.append(date)
I nodata_max < nodata & nodata > 0
nodata_max = nodata
nodata_maxline = [date]
nodata = 0
tot_line += datum
num_line++
 
tot_file += tot_line
num_file += num_line
 
print(‘Line: #11 Reject: #2 Accept: #2 Line_tot: #6.3 Line_avg: #6.3’.format(
date, data.len - num_line, num_line, tot_line, I (num_line > 0) {tot_line / num_line} E 0))
 
print()
print(‘File(s) = #.’.format(:argv[1]))
print(‘Total = #6.3’.format(tot_file))
print(‘Readings = #6’.format(num_file))
print(‘Average = #6.3’.format(tot_file / num_file))
print("\nMaximum run(s) of #. consecutive false readings ends at line starting with date(s): #.".format(nodata_max, nodata_maxline.join(‘, ’)))</syntaxhighlight>
 
{{out}}
<pre>
...
Line: 2004-12-29 Reject: 1 Accept: 23 Line_tot: 56.300 Line_avg: 2.448
Line: 2004-12-30 Reject: 1 Accept: 23 Line_tot: 65.300 Line_avg: 2.839
Line: 2004-12-31 Reject: 1 Accept: 23 Line_tot: 47.300 Line_avg: 2.057
 
File(s) = readings.txt
Total = 1358393.400
Readings = 129403
Average = 10.497
 
Maximum run(s) of 589 consecutive false readings ends at line starting with date(s): 1993-03-05
</pre>
 
=={{header|Ada}}==
{{libheader|Simple components for Ada}}
<langsyntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
with Strings_Edit; use Strings_Edit;
with Strings_Edit.Floats; use Strings_Edit.Floats;
Line 112 ⟶ 172:
Close (File);
Put_Line ("Syntax error at " & Image (Current.Line) & ':' & Image (Max.Pointer));
end Data_Munging;</langsyntaxhighlight>
The implementation performs minimal checks. The average is calculated over all valid data. For the maximal chain of consequent invalid data, the source line number, the column number, and the time stamp of the first invalid data is printed.
{{out|Sample output}}
Line 121 ⟶ 181:
 
=={{header|Aime}}==
<langsyntaxhighlight lang="aime">integer bads, count, max_bads;
file f;
list l;
Line 155 ⟶ 215:
 
o_form("Averaged /d3/ over ~ readings.\n", s / count, count);
o_("Longest bad run ", max_bads, ", started ", worst_day, ".\n");</langsyntaxhighlight>
Run as:
<pre>cat readings.txt | tr -d \\r | aime SOURCE_FILE</pre>
Line 167 ⟶ 227:
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}}
<!--{{does not work with|ELLA ALGOL 68|Any (with appropriate job cards) - argc and argv are extensions}} -->
<langsyntaxhighlight lang="algol68">INT no data := 0; # Current run of consecutive flags<0 in lines of file #
INT no data max := -1; # Max consecutive flags<0 in lines of file #
FLEX[0]STRING no data max line; # ... and line number(s) where it occurs #
Line 261 ⟶ 321:
upb list := UPB no data max line;
printf(($l"Maximum run"f(p)" of "g(-0)" consecutive false reading"f(p)" ends at line starting with date"f(p)": "$,
upb list = 1, no data max, no data max = 0, upb list = 1, list repr, no data max line, $l$))</langsyntaxhighlight>
Command:
$ a68g ./Data_Munging.a68 - data
Line 282 ⟶ 342:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey"># Author AlephX Aug 17 2011
 
SetFormat, float, 4.2
Line 355 ⟶ 415:
Totavg := TotSum / TotValid
FileAppend, `n`nDays %Lines%`nMaximal wrong readings: %maxwrong% from %startwrongdate% at %startoccurrence% to %lastwrongdate% at %lastoccurrence%`n`n, %result%
FileAppend, Valid readings: %TotValid%`nTotal Value: %TotSUm%`nAverage: %TotAvg%, %result%</langsyntaxhighlight>
{{out|Sample output}}
<pre>Day: 1990-01-01 sum: 590.00 avg: 26.82 Readings: 22/24.00
Line 374 ⟶ 434:
 
=={{header|AWK}}==
<langsyntaxhighlight lang="awk">BEGIN{
nodata = 0; # Current run of consecutive flags<0 in lines of file
nodata_max=-1; # Max consecutive flags<0 in lines of file
Line 436 ⟶ 496:
 
printf "\nMaximum run(s) of %i consecutive false readings ends at line starting with date(s): %s\n", nodata_max, nodata_maxline
}</langsyntaxhighlight>
{{out|Sample output}}
<pre>bash$ awk -f readings.awk readings.txt | tail
Line 452 ⟶ 512:
 
=={{header|Batch File}}==
<langsyntaxhighlight lang="dos">@echo off
setlocal ENABLEDELAYEDEXPANSION
set maxrun= 0
Line 518 ⟶ 578:
echo Line: %date% Accept: %count:~-3% tot: %sum:~-8% avg: %mean:~-8%
 
goto :EOF</langsyntaxhighlight>
{{out}}
<pre>
Line 540 ⟶ 600:
 
=={{header|BBC BASIC}}==
<langsyntaxhighlight lang="bbcbasic"> file% = OPENIN("readings.txt")
IF file% = 0 THEN PRINT "Could not open test data file" : END
Line 582 ⟶ 642:
PRINT "Overall mean = " ; Total / Count%
@% = &90A
PRINT '"Longest run of bad readings = " ; BadMax% " ending " BadDate$</langsyntaxhighlight>
{{out}}
<pre>
Line 605 ⟶ 665:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 689 ⟶ 749:
fclose(outfile);
return 0;
}</langsyntaxhighlight>
{{out|Sample output}}
<pre>1990-01-01 Reject: 2 Accept: 22 Average: 26.818
Line 706 ⟶ 766:
 
=={{header|C++}}==
<langsyntaxhighlight Cpplang="cpp">#include <iostream>
#include <fstream>
#include <string>
Line 772 ⟶ 832:
cout << "Maximum number of consecutive bad readings is " << badCountMax << endl;
cout << "Ends on date " << badDate << endl;
}</langsyntaxhighlight>
{{out}}
<pre>1990-01-01 Reject: 2 Accept: 22 Average: 26.818
Line 785 ⟶ 845:
 
=={{header|Clojure}}==
<syntaxhighlight lang="clojure">
<lang Clojure>
(ns rosettacode.textprocessing1
(:require [clojure.string :as str]))
Line 849 ⟶ 909:
(count max-gap)
(:date (first max-gap))))))
</syntaxhighlight>
</lang>
 
{{out}}
Line 871 ⟶ 931:
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. data-munging.
 
Line 1,014 ⟶ 1,074:
 
GOBACK
.</langsyntaxhighlight>
 
{{Out|Example output}}
Line 1,037 ⟶ 1,097:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defvar *invalid-count*)
(defvar *max-invalid*)
(defvar *max-invalid-date*)
Line 1,084 ⟶ 1,144:
(format t "~%Maximum run(s) of ~a consecutive false readings ends at ~
line starting with date(s): ~a~%"
*max-invalid* *max-invalid-date*)))</langsyntaxhighlight>
{{out|Example output}}
<pre>...
Line 1,100 ⟶ 1,160:
=={{header|D}}==
{{trans|Python}}
<langsyntaxhighlight lang="d">void main(in string[] args) {
import std.stdio, std.conv, std.string;
 
Line 1,174 ⟶ 1,234:
"readings ends at line starting with date(s): %-(%s, %)",
noDataMax, noDataMaxLine);
}</langsyntaxhighlight>
The output matches that of the [[#Python|Python]] version.
 
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
<lang Eiffel>
class
APPLICATION
Line 1,298 ⟶ 1,358:
 
end
</syntaxhighlight>
</lang>
{{out}}
Only the last three lines of the per line summary statistics are shown.
Line 1,319 ⟶ 1,379:
The function file_contents/1 is used by [[Text_processing/2]]. Please update the user if you make any interface changes.
 
<syntaxhighlight lang="erlang">
<lang Erlang>
-module( text_processing ).
 
Line 1,386 ⟶ 1,446:
{_Previous, Value_flags} = lists:foldr( fun file_content_line_value_flag/2, {[], []}, Rest ), % Preserve order
{binary:bin_to_list( Date_binary ), Value_flags}.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,406 ⟶ 1,466:
=={{header|Forth}}==
{{works with|GNU Forth}}
<langsyntaxhighlight lang="forth">\ data munging
 
\ 1991-03-30[\t10.000\t[-]1]*24
Line 1,498 ⟶ 1,558:
total-sum f@ total-n @ .mean cr ;
 
main bye</langsyntaxhighlight>
 
=={{header|Fortran}}==
Line 1,509 ⟶ 1,569:
Incidentally, a daily average of a set of measurements may be unsuitable when data are missing, as when there is a regular pattern over a day. The N.Z. electricity supply association ruled that in calculating the ratio of daytime to nighttime usage, should there be four or more missing data in a day, then the entire day's data were to be rejected when computing the monthly or quarterly ratio.
 
<syntaxhighlight lang="fortran">
<lang Fortran>
Crunches a set of hourly data. Starts with a date, then 24 pairs of value,indicator for that day, on one line.
INTEGER Y,M,D !Year, month, and day.
Line 1,602 ⟶ 1,662:
900 CLOSE(IN) !Done.
END !Spaghetti rules.
</syntaxhighlight>
</lang>
 
Output:
Line 1,617 ⟶ 1,677:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,707 ⟶ 1,767:
maxRun, maxDate)
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,725 ⟶ 1,785:
 
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">import Data.List
import Numeric
import Control.Arrow
Line 1,771 ⟶ 1,831:
(\(t,n) -> printf totalFmt n t (t/fromIntegral n)) $ fst summ
mapM_ ((\(l, d1,d2) -> printf maxFmt l d1 d2)
. (\(a,b)-> (a,(fst.(dat!!).(`div`24))b,(fst.(dat!!).(`div`24))(a+b)))) $ snd summ</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight Haskelllang="haskell">*Main> :main ["./RC/readings.txt"]</langsyntaxhighlight>
<pre>Some lines:
 
Line 1,786 ⟶ 1,846:
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight Iconlang="icon">record badrun(count,fromdate,todate) # record to track bad runs
 
procedure main()
Line 1,836 ⟶ 1,896:
else
write(fout,"No bad runs of data")
end</langsyntaxhighlight>
{{out|Sample output}}
<pre>...
Line 1,852 ⟶ 1,912:
=={{header|J}}==
'''Solution:'''
<langsyntaxhighlight lang="j"> load 'files'
parseLine=: 10&({. ,&< (_99&".;._1)@:}.) NB. custom parser
summarize=: # , +/ , +/ % # NB. count,sum,mean
Line 1,865 ⟶ 1,925:
589
]StartDates=: Dates {~ (>:@I.@e.&MaxRun (24 <.@%~ +/)@{. ]) RunLengths
1993-03-05</langsyntaxhighlight>
'''Formatting Output'''<br>
Define report formatting verbs:
<langsyntaxhighlight lang="j">formatDailySumry=: dyad define
labels=. , ];.2 'Line: Accept: Line_tot: Line_avg: '
labels , x ,. 7j0 10j3 10j3 ": y
Line 1,878 ⟶ 1,938:
'maxrun dates'=. x
out=. out,LF,'Maximum run(s) of ',(": maxrun),' consecutive false readings ends at line(s) starting with date(s): ',dates
)</langsyntaxhighlight>
{{out|Show output}}
<langsyntaxhighlight lang="j"> (_4{.Dates) formatDailySumry _4{. DailySummary
Line: Accept: Line_tot: Line_avg:
2004-12-28 23 77.800 3.383
Line 1,892 ⟶ 1,952:
Average: 10.497
 
Maximum run(s) of 589 consecutive false readings ends at line(s) starting with date(s): 1993-03-05</langsyntaxhighlight>
 
=={{header|Java}}==
{{works with|Java|7}}
<langsyntaxhighlight lang="java">import java.io.File;
import java.util.*;
import static java.lang.System.out;
Line 1,984 ⟶ 2,044:
}
}
}</langsyntaxhighlight>
 
<pre>1990-01-01 out: 2 in: 22 tot: 590.000 avg: 26.818
Line 1,999 ⟶ 2,059:
=={{header|JavaScript}}==
{{works with|JScript}}
<langsyntaxhighlight lang="javascript">var filename = 'readings.txt';
var show_lines = 5;
var file_stats = {
Line 2,067 ⟶ 2,127:
function dec3(value) {
return Math.round(value * 1e3) / 1e3;
}</langsyntaxhighlight>
{{out}}
<pre>Line: 1990-01-01 Reject: 2 Accept: 22 Line_tot: 590 Line_avg: 26.818
Line 2,090 ⟶ 2,150:
 
The "foreach" syntax is:
<langsyntaxhighlight lang="jq">foreach STREAM as $row ( INITIAL; EXPRESSION; VALUE ).</langsyntaxhighlight>
The basic idea is that for each $row in STREAM, the value specified by VALUE is emitted.
 
If we wished only to produce per-line synopses of the "readings.txt"
file, the following pattern could be used:
<langsyntaxhighlight lang="jq">foreach (inputs | split("\t")) as $line (INITIAL; EXPRESSION; VALUE)</langsyntaxhighlight>
In order to distinguish the single-line synopsis from the whole-file synopsis, we will use the following pattern instead:
<langsyntaxhighlight lang="jq">foreach ((inputs | split("\t")), null) as $line (INITIAL; EXPRESSION; VALUE)</langsyntaxhighlight>
The "null" is added so that the stream of per-line values can be distinguished from the last value in the stream.
 
Line 2,103 ⟶ 2,163:
 
One point of interest in the following program is the use of JSON objects to store values. This allows mnemonic names to be used instead of local variables.
<langsyntaxhighlight lang="jq"># Input: { "max": max_run_length,
# "starts": array_of_start_line_values, # of all the maximal runs
# "start_dates": array_of_start_dates # of all the maximal runs
Line 2,173 ⟶ 2,233:
end;
 
process</langsyntaxhighlight>
 
{{out}}
<langsyntaxhighlight lang="sh">$ jq -c -n -R -r -f Text_processing_1.jq readings.txt
[22,590,2]
[24,410,0]
Line 2,182 ⟶ 2,242:
[23,47.3,1]
There is one maximal run of lines with flag<=0.
The maximal run has length 93 and starts at line 5378 and has start date 2004-09-30.</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">
using DataFrames
 
Line 2,240 ⟶ 2,300:
maxbaddate = replace("$(df[:Date][maxbadline])", r"T.+$", "")
println("The largest run of bad values is $(maxbadval), on $(maxbaddate) beginning at $(maxbadtime):00 hours.")
</syntaxhighlight>
</lang>
{{output}}
<pre>
Line 2,275 ⟶ 2,335:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.2.31
 
import java.io.File
Line 2,325 ⟶ 2,385:
println("\nMaximum run of $maxRun consecutive false readings")
println("ends at line starting with date: $finishLine")
}</langsyntaxhighlight>
 
{{out}}
Line 2,352 ⟶ 2,412:
 
=={{header|Lua}}==
<langsyntaxhighlight Lualang="lua">filename = "readings.txt"
io.input( filename )
 
Line 2,400 ⟶ 2,460:
print( string.format( "Readings: %d", file_lines ) )
print( string.format( "Average: %f", file_sum/file_cnt_data ) )
print( string.format( "Maximum %d consecutive false readings starting at %s.", max_rejected, max_rejected_date ) )</langsyntaxhighlight>
<pre>Output:
File: readings.txt
Line 2,408 ⟶ 2,468:
Maximum 589 consecutive false readings starting at 1993-02-09.</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">FileName = "Readings.txt"; data = Import[FileName,"TSV"];
 
Scan[(a=Position[#[[3;;All;;2]],1];
Print["Line:",#[[1]] ,"\tReject:", 24 - Length[a], "\t Accept:", Length[a], "\tLine_tot:",
Line 2,424 ⟶ 2,483:
Print["\nFile(s) : ",FileName,"\nTotal : ",AccountingForm@GlobalSum,"\nReadings : ",Nb,
"\nAverage : ",GlobalSum/Nb,"\n\nMaximum run(s) of ",MaxRunRecorded,
" consecutive false readings ends at line starting with date(s):",MaxRunTime]</langsyntaxhighlight>
 
<pre>Line:1990-01-01 Reject:2 Accept:22 Line_tot:590. Line_avg:26.8182
Line 2,442 ⟶ 2,501:
=={{header|Nim}}==
{{trans|Python}}
<langsyntaxhighlight lang="nim">import os, sequtils, strutils, sequtilsstrformat
 
var
nodata = 0
nodataMax = -1
nodataMaxLine: seq[string] = @[]
 
totFile = 0.0
Line 2,453 ⟶ 2,512:
 
for filename in commandLineParams():
varfor fline =in open(filename).lines:
for line in f.lines:
var
totLine = 0.0
numLine = 0
fielddata: = line.split()seq[float]
dateflags: = fieldseq[0int]
data: seq[float] = @[]
flags: seq[int] = @[]
 
let fields = line.split()
for i, f in field[1 .. -1]:
let date = fields[0]
if i mod 2 == 0: data.add parseFloat(f)
else: flags.add parseInt(f)
 
for datumi, flagfield in items(zip(data, flags))fields[1..^1]:
if i mod 2 == 0: data.add parseFloat(field)
else: flags.add parseInt(field)
 
for datum, flag in zip(data, flags).items:
if flag < 1:
inc nodata
Line 2,483 ⟶ 2,542:
numFile += numLine
 
echolet "Line:average $#= if Reject:numLine $#> Accept0: $#totLine / LineTot: $#float(numLine) LineAvgelse: $#"0.0
echo &"Line: .format({date,} Reject: {data.len - numLine,:2} Accept: {numLine:2} ",
&"LineTot: formatFloat({totLine, precision =:6.2f} 0),LineAvg: formatFloat({average:4.2f}"
(if numLine > 0: totLine / float(numLine) else: 0.0), precision = 0))
 
echo()
echo &"""File(s) = {commandLineParams().join(" ")}"""
echo &"Total = {totFile:.2f}"
echo &"Readings = {numFile}"
echo &"Average = {totFile / float(numFile):.2f}"
echo ""
echo &"Maximum run(s) of {nodataMax} consecutive false readings ",
echo "File(s) = ", commandLineParams().join(" ")
&"""ends at line starting with date(s): {nodataMaxLine.join(" ")}."""</syntaxhighlight>
echo "Total = ", formatFloat(totFile, precision = 0)
 
echo "Readings = ", numFile
{{out}}
echo "Average = ", formatFloat(totFile / float(numFile), precision = 0)
<pre>$ ./textproc1 readings.txt | tail
echo ""
Line: 2004-12-29 Reject: 1 Accept: 23 LineTot: 56.30 LineAvg: 2.45
echo "Maximum run(s) of ", nodataMax, " consecutive false readings ends at line starting with date(s): ", nodataMaxLine.join(" ")</lang>
Line: 2004-12-30 Reject: 1 Accept: 23 LineTot: 65.30 LineAvg: 2.84
Output:
Line: 2004-12-31 Reject: 1 Accept: 23 LineTot: 47.30 LineAvg: 2.06
<pre>$ ./textproc1 readings.txt|tail
Line: 2004-12-29 Reject: 1 Accept: 23 LineTot: 56.3 LineAvg: 2.44783
Line: 2004-12-30 Reject: 1 Accept: 23 LineTot: 65.3 LineAvg: 2.83913
Line: 2004-12-31 Reject: 1 Accept: 23 LineTot: 47.3 LineAvg: 2.05652
 
File(s) = readings.txt
Total = 11358393.35839e+0640
Readings = 129403
Average = 10.497450
 
Maximum run(s) of 589 consecutive false readings ends at line starting with date(s): 1993-03-05.</pre>
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml">let input_line ic =
try Some(input_line ic)
with End_of_file -> None
Line 2,575 ⟶ 2,635:
Printf.printf "Maximum run(s) of %d consecutive false readings \
ends at line starting with date(s): %s\n"
nodata_max (String.concat ", " nodata_maxline);</langsyntaxhighlight>
 
=={{header|Perl}}==
===An AWK-like solution===
<langsyntaxhighlight lang="perl">use strict;
use warnings;
 
Line 2,637 ⟶ 2,697:
printf "\nMaximum run(s) of %i consecutive false readings ends at line starting with date(s): %s\n",
$nodata_max, $nodata_maxline;</langsyntaxhighlight>
{{out|Sample output}}
<pre>bash$ perl -f readings.pl readings.txt | tail
Line 2,653 ⟶ 2,713:
 
===An object-oriented solution===
<langsyntaxhighlight lang="perl">use strict;
use warnings;
 
Line 2,767 ⟶ 2,827:
$parser->_push_bad_range_if_necessary
}
}</langsyntaxhighlight>
{{out|Sample output}}
<pre>$ perl readings.pl < readings.txt | tail
Line 2,784 ⟶ 2,844:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>constant lines = read_lines("demo\\rosetta\\readings.txt")
<span style="color: #000080;font-style:italic;">-- demo\rosetta\TextProcessing1.exw</span>
 
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> <span style="color: #000080;font-style:italic;">-- (include version/first of next three lines only)</span>
include builtins\timedate.e
<span style="color: #008080;">include</span> <span style="color: #000000;">readings</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> <span style="color: #000080;font-style:italic;">-- global constant lines, or:
 
--assert(write_lines("readings.txt",lines)!=-1) -- first run, then:
integer count = 0,
--constant lines = read_lines("readings.txt")</span>
max_count = 0,
ntot = 0
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">\</span><span style="color: #004080;">timedate</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
atom readtot = 0
timedate run_start, max_start
<span style="color: #004080;">integer</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span>
 
<span style="color: #000000;">max_count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span>
procedure end_bad_run()
<span style="color: #000000;">ntot</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
if count then
<span style="color: #004080;">atom</span> <span style="color: #000000;">readtot</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
if count>max_count then
<span style="color: #004080;">timedate</span> <span style="color: #000000;">run_start</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">max_start</span>
max_count = count
max_start = run_start
<span style="color: #008080;">procedure</span> <span style="color: #000000;">end_bad_run</span><span style="color: #0000FF;">()</span>
end if
<span style="color: #008080;">if</span> <span style="color: #000000;">count</span> <span style="color: #008080;">then</span>
count = 0
<span style="color: #008080;">if</span> <span style="color: #000000;">count</span><span style="color: #0000FF;">></span><span style="color: #000000;">max_count</span> <span style="color: #008080;">then</span>
end if
<span style="color: #000000;">max_count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">count</span>
end procedure
<span style="color: #000000;">max_start</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">run_start</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
for i=1 to length(lines) do
<span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
sequence oneline = split(lines[i],'\t',no_empty:=true), r
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if length(oneline)!=49 then
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
?"bad line (length!=49)"
else
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
r = parse_date_string(oneline[1],{"YYYY-MM-DD"})
<span style="color: #004080;">sequence</span> <span style="color: #000000;">oneline</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #008000;">'\t'</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">r</span>
if not timedate(r) then
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">oneline</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">49</span> <span style="color: #008080;">then</span>
?{"bad date",oneline[1]}
<span style="color: #0000FF;">?</span><span style="color: #008000;">"bad line (length!=49)"</span>
else
<span style="color: #008080;">else</span>
timedate td = r
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">parse_date_string</span><span style="color: #0000FF;">(</span><span style="color: #000000;">oneline</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],{</span><span style="color: #008000;">"YYYY-MM-DD"</span><span style="color: #0000FF;">})</span>
integer rejects=0, accepts=0
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #004080;">timedate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
atom readsum = 0
<span style="color: #0000FF;">?{</span><span style="color: #008000;">"bad date"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">oneline</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]}</span>
for j=2 to 48 by 2 do
<span style="color: #008080;">else</span>
r = scanf(oneline[j],"%f")
<span style="color: #004080;">timedate</span> <span style="color: #000000;">td</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">r</span>
if length(r)!=1 then
<span style="color: #004080;">integer</span> <span style="color: #000000;">rejects</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">accepts</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span>
?{"error scanning",oneline[j]}
<span style="color: #004080;">atom</span> <span style="color: #000000;">readsum</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
rejects += 1
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">48</span> <span style="color: #008080;">by</span> <span style="color: #000000;">2</span> <span style="color: #008080;">do</span>
else
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">scanf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">oneline</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">],</span><span style="color: #008000;">"%f"</span><span style="color: #0000FF;">)</span>
atom reading = r[1][1]
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
r = scanf(oneline[j+1],"%d")
<span style="color: #0000FF;">?{</span><span style="color: #008000;">"error scanning"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">oneline</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]}</span>
if length(r)!=1 then
<span style="color: #000000;">rejects</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
?{"error scanning",oneline[j+1]}
<span rejects +style="color: 1#008080;">else</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">reading</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
else
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">scanf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">oneline</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">)</span>
integer flag = r[1][1]
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
if flag<=0 then
<span style="color: #0000FF;">?{</span><span style="color: #008000;">"error scanning"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">oneline</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]}</span>
if count=0 then
<span style="color: #000000;">rejects</span> <span style="color: #0000FF;">+=</span> <span run_start style="color: td#000000;">1</span>
<span style="color: end if#008080;">else</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">flag</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
count += 1
<span style="color: #008080;">if</span> <span style="color: #000000;">flag</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
rejects += 1
<span style="color: #008080;">if</span> <span style="color: #000000;">count</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
else
<span style="color: #000000;">run_start</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">td</span>
end_bad_run()
accepts +<span style="color: #008080;">end</span> <span style="color: 1#008080;">if</span>
readsum <span style="color: #000000;">count</span> <span style="color: #0000FF;">+=</span> <span style="color: reading#000000;">1</span>
<span style="color: #000000;">rejects</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
end if
end if <span style="color: #008080;">else</span>
<span style="color: #000000;">end_bad_run</span><span style="color: #0000FF;">()</span>
end if
<span style="color: #000000;">accepts</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
end for
<span style="color: #000000;">readsum</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">reading</span>
readtot += readsum
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
ntot += accepts
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
string average = iff(accepts=0?"N/A":sprintf("%6.3f",readsum/accepts))
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
printf(1,"Date: %s, Rejects: %2d, Accepts: %2d, Line total: %7.3f, Average %s\n",
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
{format_timedate(td,"DD/MM/YYYY"),rejects, accepts, readsum, average})
<span style="color: #000000;">readtot</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">readsum</span>
end if
<span style="color: #000000;">ntot</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">accepts</span>
end if
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">>=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">2</span> <span style="color: #008080;">then</span>
end for
<span style="color: #004080;">string</span> <span style="color: #000000;">average</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">accepts</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"N/A"</span><span style="color: #0000FF;">:</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%6.3f"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">readsum</span><span style="color: #0000FF;">/</span><span style="color: #000000;">accepts</span><span style="color: #0000FF;">))</span>
 
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Date: %s, Rejects: %2d, Accepts: %2d, Line total: %7.3f, Average %s\n"</span><span style="color: #0000FF;">,</span>
printf(1,"Average: %.3f (of %d readings)\n",{readtot/ntot,ntot})
<span style="color: #0000FF;">{</span><span style="color: #7060A8;">format_timedate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">td</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"DD/MM/YYYY"</span><span style="color: #0000FF;">),</span><span style="color: #000000;">rejects</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">accepts</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">readsum</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">average</span><span style="color: #0000FF;">})</span>
end_bad_run()
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if max_count then
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
printf(1,"Maximum run of %d consecutive false readings starting: %s\n",
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
{max_count,format_timedate(max_start,"DD/MM/YYYY")})
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end if</lang>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Average: %.3f (of %d readings)\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">readtot</span><span style="color: #0000FF;">/</span><span style="color: #000000;">ntot</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ntot</span><span style="color: #0000FF;">})</span>
<span style="color: #000000;">end_bad_run</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">max_count</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Maximum run of %d consecutive false readings starting: %s\n"</span><span style="color: #0000FF;">,</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">max_count</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">format_timedate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">max_start</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"DD/MM/YYYY"</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #0000FF;">?</span><span style="color: #008000;">"done"</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 2,866 ⟶ 2,937:
Maximum run of 589 consecutive false readings starting: 09/02/1993
</pre>
 
=={{header|Picat}}==
{{trans|Ruby}}
<syntaxhighlight lang="picat">go =>
File = "readings.txt",
Total = new_map([num_readings=0,num_good_readings=0,sum_readings=0.0]),
InvalidCount = 0,
MaxInvalidCount = 0,
InvalidRunEnd = "",
 
Id = 0,
foreach(Line in read_file_lines(File))
Id := Id + 1,
NumReadings = 0,
NumGoodReadings = 0,
SumReadings = 0,
 
Fields = Line.split,
Rec = Fields.tail.map(to_float),
foreach([Reading,Flag] in chunks_of(Rec,2))
NumReadings := NumReadings + 1,
if Flag > 0 then
NumGoodReadings := NumGoodReadings + 1,
SumReadings := SumReadings + Reading,
InvalidCount := 0
else
InvalidCount := InvalidCount + 1,
if InvalidCount > MaxInvalidCount then
MaxInvalidCount := InvalidCount,
InvalidRunEnd := Fields[1]
end
end
end,
 
Total.put(num_readings,Total.get(num_readings) + NumReadings),
Total.put(num_good_readings,Total.get(num_good_readings) + NumGoodReadings),
Total.put(sum_readings,Total.get(sum_readings) + SumReadings),
if Id <= 3 then
printf("date:%w accept:%w reject:%w sum:%w\n", Fields[1],NumGoodReadings,
NumReadings-NumGoodReadings,
SumReadings)
end
end,
nl,
printf("readings: %d good readings: %d sum: %0.3f avg: %0.3f\n",Total.get(num_readings),
Total.get(num_good_readings),
Total.get(sum_readings),
Total.get(sum_readings) / Total.get(num_good_readings)),
nl,
println(maxInvalidCount=MaxInvalidCount),
println(invalidRunEnd=InvalidRunEnd),
 
nl.</syntaxhighlight>
 
{{out}}
<pre>
date:1990-01-01 accept:22 reject:2 sum:590.0
date:1990-01-02 accept:24 reject:0 sum:410.0
date:1990-01-03 accept:24 reject:0 sum:1415.0
 
readings: 131304 good readings: 129403 sum: 1358393.400 avg: 10.497
 
maxInvalidCount = 589
invalidRunEnd = 1993-03-05</pre>
 
 
=={{header|PicoLisp}}==
{{trans|AWK}}
Put the following into an executable file "readings":
<langsyntaxhighlight PicoLisplang="picolisp">#!/usr/bin/picolisp /usr/lib/picolisp/lib.l
 
(let (NoData 0 NoDataMax -1 NoDataMaxline "!" TotFile 0 NumFile 0)
Line 2,912 ⟶ 3,048:
" consecutive false readings ends at line starting with date(s): " NoDataMaxline ) ) )
 
(bye)</langsyntaxhighlight>
Then it can be called as
<pre>$ ./readings readings.txt |tail
Line 2,928 ⟶ 3,064:
 
=={{header|PL/I}}==
<langsyntaxhighlight lang="pli">text1: procedure options (main); /* 13 May 2010 */
 
declare line character (2000) varying;
Line 2,981 ⟶ 3,117:
finish_up:
end text1;</langsyntaxhighlight>
 
=={{header|PowerShell}}==
<langsyntaxhighlight PowerShelllang="powershell">$file = '.\readings.txt'
$lines = Get-Content $file # $args[0]
$valid = $true
Line 3,037 ⟶ 3,173:
"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
}</langsyntaxhighlight>
<pre>Line : 2004-12-29
Reject : 1
Line 3,067 ⟶ 3,203:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">#TASK="Text processing/1"
Define File$, InLine$, Part$, i, Out$, ErrEnds$, Errcnt, ErrMax
Define lsum.d, tsum.d, rejects, val.d, readings
Line 3,113 ⟶ 3,249:
;
Print("Press ENTER to exit"): Input()
EndIf</langsyntaxhighlight>
{{out|Sample output}}
<pre>...
Line 3,130 ⟶ 3,266:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">import fileinput
import sys
 
Line 3,185 ⟶ 3,321:
 
print "\nMaximum run(s) of %i consecutive false readings ends at line starting with date(s): %s" % (
nodata_max, ", ".join(nodata_maxline))</langsyntaxhighlight>
{{out|Sample output}}
<pre>bash$ /cygdrive/c/Python26/python readings.py readings.txt|tail
Line 3,201 ⟶ 3,337:
 
=={{header|R}}==
<langsyntaxhighlight Rlang="r">#Read in data from file
dfr <- read.delim("readings.txt")
#Calculate daily means
Line 3,209 ⟶ 3,345:
#Calculate time between good measurements
times <- strptime(dfr[1,1], "%Y-%m-%d", tz="GMT") + 3600*seq(1,24*nrow(dfr),1)
hours.between.good.measurements <- diff(times[t(flags)])/3600</langsyntaxhighlight>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
;; Use SRFI 48 to make %n.nf formats convenient.
(require (prefix-in srfi/48: srfi/48)) ; SRFI 48: Intermediate Format Strings
Line 3,265 ⟶ 3,401:
(unless (zero? N) (srfi/48:format #t "Average = ~10,3F~%" (/ sum N)))
(srfi/48:format #t "~%Maximum run(s) of ~a consecutive false readings ends at line starting with date(s): ~a~%"
max-consecutive-false (string-join max-false-tags))))</langsyntaxhighlight>
{{out|Sample run}}
<pre>$ racket 1.rkt readings/readings.txt | tail
Line 3,281 ⟶ 3,417:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>my @gaps;
my $previous = 'valid';
 
Line 3,313 ⟶ 3,449:
 
say "Longest period of invalid readings was {$longest<count>} hours,\n",
"from {$longest<start>} till {$longest<end>}."</langsyntaxhighlight>
{{out}}
<pre>
Line 3,334 ⟶ 3,470:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program to process instrument data from a data file. */
numeric digits 20 /*allow for bigger (precision) numbers.*/
ifid='READINGS.TXT' /*the name of the input file. */
Line 3,398 ⟶ 3,534:
do j=e to b by -3; _=insert(',',_,j); end /*j*/; return _
/*────────────────────────────────────────────────────────────────────────────*/
sy: say arg(1); call lineout ofid,arg(1); return</langsyntaxhighlight>
'''output''' &nbsp; when using the default input file:
<pre style="height:40ex">
Line 3,421 ⟶ 3,557:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">filename = "readings.txt"
total = { "num_readings" => 0, "num_good_readings" => 0, "sum_readings" => 0.0 }
invalid_count = 0
Line 3,463 ⟶ 3,599:
printf "Average = %.3f\n", total['sum_readings']/total['num_good_readings']
puts ""
puts "Maximum run(s) of #{max_invalid_count} consecutive false readings ends at #{invalid_run_end}"</langsyntaxhighlight>
 
Alternate implementation:
<langsyntaxhighlight lang="ruby">Reading = Struct.new(:date, :value, :flag)
 
DailyReading = Struct.new(:date, :readings) do
Line 3,498 ⟶ 3,634:
puts
puts "Max run of #{worst_streak.count} consecutive false readings from #{worst_streak.first.date} until #{worst_streak.last.date}"
</syntaxhighlight>
</lang>
 
=={{header|Scala}}==
Line 3,504 ⟶ 3,640:
 
A fully functional solution, minus the fact that it uses iterators:
<langsyntaxhighlight lang="scala">object DataMunging {
import scala.io.Source
Line 3,561 ⟶ 3,697:
println(report format (files mkString " ", totalSum, totalSize, totalSum / totalSize, invalidCount, startDate))
}
}</langsyntaxhighlight>
A quick&dirty solution:
<langsyntaxhighlight lang="scala">object AltDataMunging {
def main(args: Array[String]) {
var totalSum = 0.0
Line 3,604 ⟶ 3,740:
println(report format (files mkString " ", totalSum, totalSize, totalSum / totalSize, maxInvalidCount, maxInvalidDate))
}
}</langsyntaxhighlight>
Last few lines of the sample output (either version):
<pre>
Line 3,622 ⟶ 3,758:
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">var gaps = [];
var previous = :valid;
 
Line 3,629 ⟶ 3,765:
var valid = [];
var hour = 0;
readings.map{.to_n}.each_slice(2, { |slicereading, flag|
var(reading, flag) = slice...;
if (flag > 0) {
valid << reading;
Line 3,654 ⟶ 3,789:
 
say ("Longest period of invalid readings was #{longest{:count}} hours,\n",
"from #{longest{:start}} till #{longest{:end}}.");</langsyntaxhighlight>
{{out}}
<pre>
Line 3,667 ⟶ 3,802:
</pre>
''Output is from the sample of the task.''
 
=={{header|Swift}}==
 
{{trans|Rust}}
 
<syntaxhighlight lang="swift">import Foundation
 
let fmtDbl = { String(format: "%10.3f", $0) }
 
Task.detached {
let formatter = DateFormatter()
 
formatter.dateFormat = "yyyy-MM-dd"
 
let (data, _) = try await URLSession.shared.bytes(from: URL(fileURLWithPath: CommandLine.arguments[1]))
var rowStats = [(Date, Double, Int)]()
var invalidPeriods = 0
var invalidStart: Date?
var sumFile = 0.0
var readings = 0
var longestInvalid = 0
var longestInvalidStart: Date?
var longestInvalidEnd: Date?
 
for try await line in data.lines {
let lineSplit = line.components(separatedBy: "\t")
 
guard !lineSplit.isEmpty, let date = formatter.date(from: lineSplit[0]) else {
fatalError("Invalid date \(lineSplit[0])")
}
 
let data = Array(lineSplit.dropFirst())
let parsed = stride(from: 0, to: data.endIndex, by: 2).map({idx -> (Double, Int) in
let slice = data[idx..<idx+2]
 
return (Double(slice[idx]) ?? 0, Int(slice[idx+1]) ?? 0)
})
 
var sum = 0.0
var numValid = 0
 
for (val, flag) in parsed {
if flag <= 0 {
if invalidStart == nil {
invalidStart = date
}
 
invalidPeriods += 1
} else {
if invalidPeriods > longestInvalid {
longestInvalid = invalidPeriods
longestInvalidStart = invalidStart
longestInvalidEnd = date
}
 
sumFile += val
sum += val
numValid += 1
readings += 1
invalidPeriods = 0
invalidStart = nil
}
}
 
if numValid != 0 {
rowStats.append((date, sum / Double(numValid), parsed.count - numValid))
}
}
 
for stat in rowStats.lazy.reversed().prefix(5) {
print("\(stat.0): Average: \(fmtDbl(stat.1)); Valid Readings: \(24 - stat.2); Invalid Readings: \(stat.2)")
}
 
print("""
 
Sum File: \(fmtDbl(sumFile))
Average: \(fmtDbl(sumFile / Double(readings)))
Readings: \(readings)
Longest Invalid: \(longestInvalid) (\(longestInvalidStart!) - \(longestInvalidEnd!))
""")
 
exit(0)
}
 
dispatchMain()
</syntaxhighlight>
 
{{out}}
 
<pre>2004-12-31 05:00:00 +0000: Average: 2.057; Valid Readings: 23; Invalid Readings: 1
2004-12-30 05:00:00 +0000: Average: 2.839; Valid Readings: 23; Invalid Readings: 1
2004-12-29 05:00:00 +0000: Average: 2.448; Valid Readings: 23; Invalid Readings: 1
2004-12-28 05:00:00 +0000: Average: 3.383; Valid Readings: 23; Invalid Readings: 1
2004-12-27 05:00:00 +0000: Average: 2.483; Valid Readings: 23; Invalid Readings: 1
 
Sum File: 1358393.400
Average: 10.497
Readings: 129403
Longest Invalid: 589 (1993-02-09 05:00:00 +0000 - 1993-03-05 05:00:00 +0000)</pre>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">set max_invalid_run 0
set max_invalid_run_end ""
set tot_file 0
Line 3,709 ⟶ 3,943:
puts "Average = [format %.3f [expr {$tot_file / $num_file}]]"
puts ""
puts "Maximum run(s) of $max_invalid_run consecutive false readings ends at $max_invalid_run_end"</langsyntaxhighlight>
 
=={{header|Ursala}}==
Line 3,715 ⟶ 3,949:
and booleans (type <code>%ebXLm</code>) in the parsed data. The same function is used to compute the daily and the
cumulative statistics.
<langsyntaxhighlight Ursalalang="ursala">#import std
#import nat
#import flo
Line 3,735 ⟶ 3,969:
@nmrSPDSL -&~&,leql$^; ^/length ~&zn&-@hrZPF+ rlc both ~&rZ+-
 
main = ^T(daily_stats^lrNCT/~& @mSL 'summary ':,long_run) parsed_data</langsyntaxhighlight>
last few lines of output:
<pre>
Line 3,746 ⟶ 3,980:
 
=={{header|VBScript}}==
<langsyntaxhighlight lang="vb">Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(objFSO.GetParentFolderName(WScript.ScriptFullName) &_
"\data.txt",1)
Line 3,808 ⟶ 4,042:
WScript.StdOut.WriteLine
objFile.Close
Set objFSO = Nothing</langsyntaxhighlight>
{{Out}}
<pre>
Line 3,824 ⟶ 4,058:
{{trans|AWK}}
Vedit does not have floating point data type, so fixed point calculations are used here.
<langsyntaxhighlight lang="vedit">#50 = Buf_Num // Current edit buffer (source data)
File_Open("output.txt")
#51 = Buf_Num // Edit buffer for output file
Line 3,891 ⟶ 4,125:
IT("Maximum run(s) of ") Num_Ins(#13, LEFT+NOCR)
IT(" consecutive false readings ends at line starting with date(s): ") Reg_Ins(15)
IN</langsyntaxhighlight>
{{out|Sample output}}
<pre>
Line 3,910 ⟶ 4,144:
{{libheader|Wren-pattern}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "io" for File
import "./pattern" for Pattern
import "./fmt" for Fmt
 
var p = Pattern.new("+1/s")
Line 3,963 ⟶ 4,197:
Fmt.print("Average = $0.3f", average)
Fmt.print("\nMaximum run of $d consecutive false readings", maxRun)
Fmt.print("ends at line starting with date: $s", finishLine)</langsyntaxhighlight>
 
{{out}}
9,476

edits