Text processing/1: Difference between revisions
Content added Content deleted
(Added Wren) |
|||
Line 3,905: | Line 3,905: | ||
Maximum run(s) of 589 consecutive false readings ends at line starting with date(s): 1993-03-05 |
Maximum run(s) of 589 consecutive false readings ends at line starting with date(s): 1993-03-05 |
||
</pre> |
</pre> |
||
=={{header|Wren}}== |
|||
{{trans|Kotlin}} |
|||
{{libheader|Wren-pattern}} |
|||
{{libheader|Wren-fmt}} |
|||
<lang ecmascript>import "io" for File |
|||
import "/pattern" for Pattern |
|||
import "/fmt" for Fmt |
|||
var p = Pattern.new("+1/s") |
|||
var fileName = "readings.txt" |
|||
var lines = File.read(fileName).trimEnd().split("\r\n") |
|||
var f = "Line: $s Reject: $2d Accept: $2d Line_tot: $8.3f Line_avg: $7.3f" |
|||
var grandTotal = 0 |
|||
var readings = 0 |
|||
var date = "" |
|||
var run = 0 |
|||
var maxRun = -1 |
|||
var finishLine = "" |
|||
for (line in lines) { |
|||
var fields = p.splitAll(line) |
|||
date = fields[0] |
|||
if (fields.count == 49) { |
|||
var accept = 0 |
|||
var total = 0 |
|||
var i = 1 |
|||
while (i < fields.count) { |
|||
if (Num.fromString(fields[i+1]) >= 1) { |
|||
accept = accept + 1 |
|||
total = total + Num.fromString(fields[i]) |
|||
if (run > maxRun) { |
|||
maxRun = run |
|||
finishLine = date |
|||
} |
|||
run = 0 |
|||
} else { |
|||
run = run + 1 |
|||
} |
|||
i = i + 2 |
|||
} |
|||
grandTotal = grandTotal + total |
|||
readings = readings + accept |
|||
Fmt.print(f, date, 24-accept, accept, total, total/accept) |
|||
} else { |
|||
Fmt.print("Line: $s does not have 49 fields and has been ignored", date) |
|||
} |
|||
} |
|||
if (run > maxRun) { |
|||
maxRun = run |
|||
finishLine = date |
|||
} |
|||
var average = grandTotal / readings |
|||
Fmt.print("\nFile = $s", fileName) |
|||
Fmt.print("Total = $0.3f", grandTotal) |
|||
Fmt.print("Readings = $d", readings) |
|||
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)</lang> |
|||
{{out}} |
|||
Abridged output. |
|||
<pre> |
|||
Line: 1990-01-01 Reject: 2 Accept: 22 Line_tot: 590.000 Line_avg: 26.818 |
|||
Line: 1990-01-02 Reject: 0 Accept: 24 Line_tot: 410.000 Line_avg: 17.083 |
|||
Line: 1990-01-03 Reject: 0 Accept: 24 Line_tot: 1415.000 Line_avg: 58.958 |
|||
Line: 1990-01-04 Reject: 0 Accept: 24 Line_tot: 1800.000 Line_avg: 75.000 |
|||
Line: 1990-01-05 Reject: 0 Accept: 24 Line_tot: 1130.000 Line_avg: 47.083 |
|||
... |
|||
Line: 2004-12-27 Reject: 1 Accept: 23 Line_tot: 57.100 Line_avg: 2.483 |
|||
Line: 2004-12-28 Reject: 1 Accept: 23 Line_tot: 77.800 Line_avg: 3.383 |
|||
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 = readings.txt |
|||
Total = 1358393.400 |
|||
Readings = 129403 |
|||
Average = 10.497 |
|||
Maximum run of 589 consecutive false readings |
|||
ends at line starting with date: 1993-03-05 |
|||
</pre> |
|||
{{omit from|Openscad}} |
{{omit from|Openscad}} |