Text processing/2: Difference between revisions

Done some formatting changes. Replaced output of number of good records with output of number of records with wrong format. Added output.
(Added Wren)
(Done some formatting changes. Replaced output of number of good records with output of number of records with wrong format. Added output.)
Line 1,805:
 
=={{header|Nim}}==
<lang Nim>import strutils, tables
import strutils, tables
 
const NumFields = 49
Line 1,812 ⟶ 1,811:
const FlagGoodValue = 1
 
var badRecords: int # the number # Number of records that have invalid formatted values.
var totalRecords: int # the total # Total number of records in the file.
var badInstruments: int # the totalTotal number of records that have at least one instrument showing error.
var seenDates: = newTableTable[string, bool]() # tableTable thatto keepskeep track of what dates we have seen.
 
proc checkFloats(floats: seq[string]): bool =
## ensureEnsure we can parse all records as floats (except the date stamp).
proc checkFloats(floats:seq[string]): bool =
for index in 1..<NumFields-1:
try:
# weWe're assuming all instrument flags are floats not integers.
discard parseFloat(floats[index])
except ValueError:
Line 1,827 ⟶ 1,826:
true
 
# ensure that all sensor flags are ok
proc areAllFlagsOk(instruments: seq[string]): bool =
## ensureEnsure that all sensor flags are ok.
#flags start at index 2, and occur every 2 fields
 
for index in countup(2,NumFields,2):
#flags Flags start at index 2, and occur every 2 fields.
# we're assuming all instrument flags are floats not integers
for index in countup(2, NumFields, 2):
# weWe're assuming all instrument flags are floats not integers
var flag = parseFloat(instruments[index])
if flag < FlagGoodValue: return false
Line 1,837:
 
 
# Note: we're not checking the format of the date stamp.
 
# mainMain.
var lines = readFile("readings.txt")
var currentLine: int
 
var currentLine: int= 0
for line in lines"readings.splitLinestxt".lines:
currentLine.inc
if line.len == 0: continue #empty Empty lines don't count as records.
if line.len == 0: continue
var tokens = line.split({' ','\t'})
 
var tokens = line.split({' ', '\t'})
totalRecords.inc
 
if tokens.len != NumFields:
badRecords.inc
continue
Line 1,866 ⟶ 1,863:
echo tokens[DateField], " duplicated on line ", currentLine
 
varlet goodRecords = totalRecords - badRecords
varlet goodInstruments = goodRecords - badInstruments
 
echo "Total Records: ", totalRecords
echo "Records with wrong format: ", badRecords
echo "Records where all instumentsinstruments were OK: ", goodInstruments</lang>
 
{{out}}
echo "Total Records:", totalRecords
<pre>1990-03-25 duplicated on line 85
echo "Good Records:", goodRecords
1991-03-31 duplicated on line 456
echo "Records where all instuments were OK:", goodInstruments
1992-03-29 duplicated on line 820
</lang>
1993-03-28 duplicated on line 1184
1995-03-26 duplicated on line 1911
Total Records: 5471
Records with wrong format: 0
Records where all instruments were OK: 5017</pre>
 
=={{header|OCaml}}==
Anonymous user