Anonymous user
Text processing/2: Difference between revisions
m
→{{header|REXX}}: added/changed whitespace and comments, simplified the COMMAS subroutine.
m (→{{header|REXX}}: added/changed whitespace and comments, simplified the COMMAS subroutine.) |
|||
Line 2,241:
=={{header|REXX}}==
This REXX program process the file mentioned in "text processing 1" and does further
<br><br>
Some of the checks performed are:
::* checks for duplicated date records.
::* checks for a bad date (YYYY-MM-DD) format, among:
::* wrong length
::* year > current year
::* year < 1970 (to allow for posthumous data)
::* mm < 1 or mm > 12
::* dd < 1 or dd > days for the month
::* yyyy, dd, mm isn't numeric
::* missing data (or flags)
::* flag isn't an integer
::* flag contains a decimal point
::* data isn't numeric
In addition, all of the presented numbers
<br><br>
The program has (negated) code to write the report to a file in addition to the console.
<lang rexx>/*REXX program to process instrument data from a data file. */
numeric digits 20 /*allow for bigger numbers. */
ifid='READINGS.TXT' /*name of the input file.
ofid='READINGS.OUT' /*
grandSum=0 /*grand sum of the whole file. */
grandOKs=0
oldDate =0 /*placeholder of
w =16 /*width of fields when displayed. */
dupDates=0 /*count of duplicated timestamps. */
badDates=0 /*count of bad dates (bad format). */
badData =0 /*count of bad
ignoredR=0 /*count of ignored records,
maxInstruments=24 /*maximum number of instruments. */
yyyyCurr=right(date(),4) /*get the current year (today). */
monDD. =31 /*number of days in every month. */
/*
monDD.4 =30
monDD.6 =30
Line 2,284:
monDD.11=30
do records=1 while lines(ifid)\==0 /*read until finished. */
rec=linein(ifid) /*read the next record (line). */
parse var rec datestamp Idata /*pick off the the dateStamp
if datestamp==oldDate then do
dupDates=dupDates+1
call sy datestamp copies('~',30),
'is a duplicate of the',
"previous datestamp."
ignoredR=ignoredR+1 /*bump # of ignoredRecs.*/
iterate
end
parse var datestamp yyyy '-' mm '-' dd /*obtain YYYY, MM, and the DD. */
monDD.2=28+leapyear(yyyy) /*how long is February in year YYYY ? */
/*check for various bad formats. */
if verify(yyyy||mm||dd,1234567890)\==0 |,
length(datestamp)\==10 |,
Line 2,306:
yyyy<1970 |,
yyyy>yyyyCurr |,
mm=0
mm>12
badDates=badDates+1
call sy datestamp copies('~'),
'has an illegal format.'
ignoredR=ignoredR+1
iterate /*ignore this bad
end
oldDate=datestamp /*save datestamp for the next read. */
sum=0
flg=0
OKs=0
do j=1 until Idata='' /*process the instrument data. */
parse var Idata data.j flag.j Idata
if pos('.',flag.j)\==0 |, /*does flag have a decimal point -or- */
\datatype(flag.j,'W') then do
call sy datestamp copies('~'),
'instrument' j "has a bad flag:",
flag.j
iterate /*ignore it
end
if \datatype(data.j,'N') then do
badData=badData+1 /*bump counter.*/
call sy datestamp copies('~'),
'instrument' j "has bad data:",
data.j
iterate /*ignore it & it's flag.*/
end
if flag.j>0 then do
OKs=OKs+1
sum=sum+data.j
if
end
else do
flg=flg+1
end
end /*j*/
if j>maxInstruments then do
badData=badData+1
call sy datestamp copies('~'),
'too many instrument datum'
end
if OKs\==0 then avg=format(sum/OKs,,3)
else avg='[n/a]'
grandOKs=grandOKs+OKs
_=right(
grandSum=grandSum+sum
grandFlg=grandFlg+flg
Line 2,370:
end /*records*/
records=records-1 /*adjust for reading
if grandOKs\==0 then grandAvg=format(grandsum/grandOKs,,3)
else grandAvg='[n/a]'
call sy
call sy copies('=',60)
call sy ' records read:' right(
call sy ' records ignored:' right(
call sy ' grand sum:' right(
call sy ' grand average:' right(
call sy ' grand OK data:' right(
call sy ' grand flagged:' right(
call sy ' duplicate dates:' right(
call sy ' bad dates:' right(
call sy ' bad data:' right(
call sy ' bad flags:' right(
if Lflag\==0 then call sy ' longest flagged:' right(
▲call sy ' longest flagged:' right(comma(longFlag),w) " ending at " longdate
call sy copies('=',60)
exit /*stick a fork in it, we're all done.*/▼
/*────────────────────────────────────────────────────────────────────────────*/
▲exit /*stick a fork in it, we're done.*/
commas: procedure; parse arg _; n=_'.9'; #=123456789; b=verify(n,#,"M")
if length(y)==2 then y=left(right(date(),4),2)y /*adjust for YY year.*/▼
/*────────────────────────────────────────────────────────────────────────────*/
if y//4\==0 then return 0 /* not ≈ by 4? Not a leapyear.*/▼
▲if length(y)==2 then y=left(right(date(),4),2)y /*adjust for YY year.*/
return y//100\==0 | y//400==0 /*apply the 100 and the 400 year rule.*/
/*────────────────────────────────────────────────────────────────────────────*/
sy: say arg(1); call lineout ofid,arg(1); return</lang>
'''output''' when using the default input file:
▲ e=verify(n,#'0',,verify(n,#"0.",'M'))-p-1;end
▲ do j=e to b by -p while k<t;_=insert(c,_,j);k=k+1;end;return _</lang>
<pre style="height:35ex">
∙
|