RosettaCodeData/Task/Text-processing-1/D/text-processing-1.d

76 lines
2.4 KiB
D
Raw Permalink Normal View History

2013-10-27 22:24:23 +00:00
void main(in string[] args) {
import std.stdio, std.conv, std.string;
2013-04-11 01:07:29 -07:00
2013-10-27 22:24:23 +00:00
const fileNames = (args.length == 1) ? ["readings.txt"] :
args[1 .. $];
2013-04-11 01:07:29 -07:00
int noData, noDataMax = -1;
string[] noDataMaxLine;
double fileTotal = 0.0;
int fileValues;
2013-10-27 22:24:23 +00:00
foreach (const fileName; fileNames) {
foreach (char[] line; fileName.File.byLine) {
2013-04-11 01:07:29 -07:00
double lineTotal = 0.0;
int lineValues;
2013-10-27 22:24:23 +00:00
// Extract field info.
const parts = line.split;
2013-04-11 01:07:29 -07:00
const date = parts[0];
const fields = parts[1 .. $];
assert(fields.length % 2 == 0,
format("Expected even number of fields, not %d.",
fields.length));
for (int i; i < fields.length; i += 2) {
2013-10-27 22:24:23 +00:00
immutable value = fields[i].to!double;
immutable flag = fields[i + 1].to!int;
2013-04-11 01:07:29 -07:00
if (flag < 1) {
noData++;
continue;
}
2013-10-27 22:24:23 +00:00
// Check run of data-absent fields.
2013-04-11 01:07:29 -07:00
if (noDataMax == noData && noData > 0)
noDataMaxLine ~= date.idup;
if (noDataMax < noData && noData > 0) {
noDataMax = noData;
noDataMaxLine.length = 1;
noDataMaxLine[0] = date.idup;
}
2013-10-27 22:24:23 +00:00
// Re-initialise run of noData counter.
2013-04-11 01:07:29 -07:00
noData = 0;
2013-10-27 22:24:23 +00:00
// Gather values for averaging.
2013-04-11 01:07:29 -07:00
lineTotal += value;
lineValues++;
}
2013-10-27 22:24:23 +00:00
// Totals for the file so far.
2013-04-11 01:07:29 -07:00
fileTotal += lineTotal;
fileValues += lineValues;
writefln("Line: %11s Reject: %2d Accept: %2d" ~
" Line_tot: %10.3f Line_avg: %10.3f",
date,
fields.length / 2 - lineValues,
lineValues,
lineTotal,
(lineValues > 0) ? lineTotal / lineValues : 0.0);
}
}
2013-10-27 22:24:23 +00:00
writefln("\nFile(s) = %-(%s, %)", fileNames);
2013-04-11 01:07:29 -07:00
writefln("Total = %10.3f", fileTotal);
writefln("Readings = %6d", fileValues);
writefln("Average = %10.3f", fileTotal / fileValues);
writefln("\nMaximum run(s) of %d consecutive false " ~
2013-10-27 22:24:23 +00:00
"readings ends at line starting with date(s): %-(%s, %)",
noDataMax, noDataMaxLine);
2013-04-11 01:07:29 -07:00
}