Text processing/2: Difference between revisions

Content added Content deleted
(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: Line 1,805:


=={{header|Nim}}==
=={{header|Nim}}==
<lang Nim>
<lang Nim>import strutils, tables
import strutils, tables


const NumFields = 49
const NumFields = 49
Line 1,812: Line 1,811:
const FlagGoodValue = 1
const FlagGoodValue = 1


var badRecords: int # the number of records that have invalid formatted values
var badRecords: int # Number of records that have invalid formatted values.
var totalRecords: int # the total number of records in the file
var totalRecords: int # Total number of records in the file.
var badInstruments: int # the total number of records that have at least one instrument showing error
var badInstruments: int # Total number of records that have at least one instrument showing error.
var seenDates = newTable[string,bool]() # table that keeps track of what dates we have seen
var seenDates: Table[string, bool] # Table to keep track of what dates we have seen.


proc checkFloats(floats: seq[string]): bool =
# ensure we can parse all records as floats (except the date stamp)
## Ensure we can parse all records as floats (except the date stamp).
proc checkFloats(floats:seq[string]): bool =
for index in 1..NumFields-1:
for index in 1..<NumFields:
try:
try:
# we're assuming all instrument flags are floats not integers
# We're assuming all instrument flags are floats not integers.
discard parseFloat(floats[index])
discard parseFloat(floats[index])
except ValueError:
except ValueError:
Line 1,827: Line 1,826:
true
true


# ensure that all sensor flags are ok
proc areAllFlagsOk(instruments: seq[string]): bool =
proc areAllFlagsOk(instruments: seq[string]): bool =
## Ensure that all sensor flags are ok.
#flags start at index 2, and occur every 2 fields

for index in countup(2,NumFields,2):
# 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):
# We're assuming all instrument flags are floats not integers
var flag = parseFloat(instruments[index])
var flag = parseFloat(instruments[index])
if flag < FlagGoodValue: return false
if flag < FlagGoodValue: return false
Line 1,837: Line 1,837:




# Note: we're not checking the format of the date stamp
# Note: we're not checking the format of the date stamp.


# main
# Main.
var lines = readFile("readings.txt")
var currentLine: int


var currentLine = 0
for line in lines.splitLines:
for line in "readings.txt".lines:
currentLine.inc
currentLine.inc
#empty lines don't count as records
if line.len == 0: continue # Empty lines don't count as records.
if line.len == 0: continue
var tokens = line.split({' ','\t'})


var tokens = line.split({' ', '\t'})
totalRecords.inc
totalRecords.inc


if tokens.len != NumFields:
if tokens.len != NumFields:
badRecords.inc
badRecords.inc
continue
continue
Line 1,866: Line 1,863:
echo tokens[DateField], " duplicated on line ", currentLine
echo tokens[DateField], " duplicated on line ", currentLine


var goodRecords = totalRecords - badRecords
let goodRecords = totalRecords - badRecords
var goodInstruments = goodRecords - badInstruments
let goodInstruments = goodRecords - badInstruments

echo "Total Records: ", totalRecords
echo "Records with wrong format: ", badRecords
echo "Records where all instruments 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}}==
=={{header|OCaml}}==