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

38 lines
1.3 KiB
D
Raw Permalink Normal View History

2013-04-11 01:07:29 -07:00
void main() {
2013-10-27 22:24:23 +00:00
import std.stdio, std.array, std.string, std.regex, std.conv,
std.algorithm;
auto rxDate = `^\d\d\d\d-\d\d-\d\d$`.regex;
// Works but eats lot of RAM in DMD 2.064.
// auto rxDate = ctRegex!(`^\d\d\d\d-\d\d-\d\d$`);
2013-04-11 01:07:29 -07:00
int[string] repeatedDates;
int goodReadings;
2013-10-27 22:24:23 +00:00
foreach (string line; "readings.txt".File.lines) {
2013-04-11 01:07:29 -07:00
try {
2013-10-27 22:24:23 +00:00
auto parts = line.split;
2013-04-11 01:07:29 -07:00
if (parts.length != 49)
throw new Exception("Wrong column count");
2013-10-27 22:24:23 +00:00
if (parts[0].match(rxDate).empty)
2013-04-11 01:07:29 -07:00
throw new Exception("Date is wrong");
repeatedDates[parts[0]]++;
bool noProblem = true;
for (int i = 1; i < 48; i += 2) {
2013-10-27 22:24:23 +00:00
if (parts[i + 1].to!int < 1)
2013-04-11 01:07:29 -07:00
// don't break loop because it's validation too.
noProblem = false;
2013-10-27 22:24:23 +00:00
if (!parts[i].isNumeric)
2013-04-11 01:07:29 -07:00
throw new Exception("Reading is wrong: "~parts[i]);
}
if (noProblem)
goodReadings++;
} catch(Exception ex) {
writefln(`Problem in line "%s": %s`, line, ex);
}
}
2013-10-27 22:24:23 +00:00
writefln("Duplicated timestamps: %-(%s, %)",
repeatedDates.byKey.filter!(k => repeatedDates[k] > 1));
2013-04-11 01:07:29 -07:00
writeln("Good reading records: ", goodReadings);
}