Text processing/1: Difference between revisions

Actually done the changes described in previous edit.
(Updated to work with Nim 1.4: replaced [1..-1]" with "[1..^1]. Changed output formatting to use the more friendly "strformat". Done miscellaneous other changes.)
(Actually done the changes described in previous edit.)
Line 2,442:
=={{header|Nim}}==
{{trans|Python}}
<lang nim>import os, sequtils, strutils, sequtilsstrformat
 
var
nodata = 0
nodataMax = -1
nodataMaxLine: seq[string] = @[]
 
totFile = 0.0
Line 2,453:
 
for filename in commandLineParams():
varfor fline =in open(filename).lines:
for line in f.lines:
var
totLine = 0.0
numLine = 0
fielddata: = line.split()seq[float]
dateflags: = fieldseq[0int]
data: seq[float] = @[]
flags: seq[int] = @[]
 
let fields = line.split()
for i, f in field[1 .. -1]:
let date = fields[0]
if i mod 2 == 0: data.add parseFloat(f)
else: flags.add parseInt(f)
 
for datumi, flagfield in items(zip(data, flags))fields[1..^1]:
if i mod 2 == 0: data.add parseFloat(ffield)
else: flags.add parseInt(ffield)
 
for datum, flag in zip(data, flags).items:
if flag < 1:
inc nodata
Line 2,483:
numFile += numLine
 
echolet "Line:average $#= if Reject:numLine $#> Accept0: $#totLine / LineTot: $#float(numLine) LineAvgelse: $#"0.0
echo &"Line: .format({date,} Reject: {data.len - numLine,:2} Accept: {numLine:2} ",
&"LineTot: formatFloat({totLine, precision =:6.2f} 0),LineAvg: formatFloat({average:4.2f}"
(if numLine > 0: totLine / float(numLine) else: 0.0), precision = 0))
 
echo ""()
echo &"""File(s) = ", {commandLineParams().join(" ")}"""
echo &"Total = {totFile:.2f}"
echo &"Readings = {numFile}", numFile
echo &"Average = ", formatFloat({totFile / float(numFile), precision = 0):.2f}"
echo ""
echo &"Maximum run(s) of ", {nodataMax, "} consecutive false readings ends at line starting with date(s): ", nodataMaxLine.join(" ")</lang>
echo "File(s) = ", commandLineParams().join(" ")
&"""ends at line starting with date(s): {nodataMaxLine.join(" ")}."""</lang>
echo "Total = ", formatFloat(totFile, precision = 0)
 
echo "Readings = ", numFile
{{out}}
echo "Average = ", formatFloat(totFile / float(numFile), precision = 0)
echo ""
echo "Maximum run(s) of ", nodataMax, " consecutive false readings ends at line starting with date(s): ", nodataMaxLine.join(" ")</lang>
Output:
<pre>$ ./textproc1 readings.txt | tail
Line: 2004-12-29 Reject: 1 Accept: 23 LineTot: 56.30 LineAvg: 2.45
Anonymous user