RosettaCodeData/Task/Text-processing-1/REXX/text-processing-1.rexx

66 lines
3.4 KiB
Rexx
Raw Permalink Normal View History

2015-11-18 06:14:39 +00:00
/*REXX program to process instrument data from a data file. */
numeric digits 20 /*allow for bigger (precision) numbers.*/
ifid='READINGS.TXT' /*the name of the input file. */
ofid='READINGS.OUT' /* " " " " output " */
grandSum=0 /*the grand sum of whole file. */
grandFlg=0 /*the grand number of flagged data. */
2013-04-11 01:07:29 -07:00
grandOKs=0
2015-11-18 06:14:39 +00:00
Lflag=0 /*the longest period of flagged data. */
Cflag=0 /*the longest continous flagged data. */
w=16 /*the width of fields when displayed. */
2013-04-11 01:07:29 -07:00
2015-11-18 06:14:39 +00:00
do recs=1 while lines(ifid)\==0 /*keep reading records until finished. */
rec=linein(ifid) /*read the next record (line) of file. */
parse var rec datestamp Idata /*pick off the dateStamp and the data. */
2013-04-11 01:07:29 -07:00
sum=0
flg=0
OKs=0
2015-11-18 06:14:39 +00:00
do j=1 until Idata='' /*process the instrument data. */
2013-04-11 01:07:29 -07:00
parse var Idata data.j flag.j Idata
2015-11-18 06:14:39 +00:00
if flag.j>0 then do /*process good data ··· */
2013-04-11 01:07:29 -07:00
OKs=OKs+1
sum=sum+data.j
2015-11-18 06:14:39 +00:00
if Cflag>Lflag then do
Ldate=datestamp
Lflag=Cflag
end
Cflag=0
2013-04-11 01:07:29 -07:00
end
2015-11-18 06:14:39 +00:00
else do /*process flagged data ··· */
2013-04-11 01:07:29 -07:00
flg=flg+1
2015-11-18 06:14:39 +00:00
Cflag=Cflag+1
2013-04-11 01:07:29 -07:00
end
end /*j*/
2015-11-18 06:14:39 +00:00
if OKs\==0 then avg=format(sum/OKs,,3)
else avg='[n/a]'
2013-04-11 01:07:29 -07:00
grandOKs=grandOKs+OKs
2015-11-18 06:14:39 +00:00
_=right(commas(avg),w)
2013-04-11 01:07:29 -07:00
grandSum=grandSum+sum
grandFlg=grandFlg+flg
2015-11-18 06:14:39 +00:00
if flg==0 then call sy datestamp ' average='_
else call sy datestamp ' average='_ ' flagged='right(flg,2)
2013-04-11 01:07:29 -07:00
end /*recs*/
2015-11-18 06:14:39 +00:00
recs=recs-1 /*adjust for reading the end─of─file. */
if grandOKs\==0 then Gavg=format(grandSum/grandOKs,,3)
2013-04-11 01:07:29 -07:00
else Gavg='[n/a]'
call sy
call sy copies('',60)
2015-11-18 06:14:39 +00:00
call sy ' records read:' right(commas(recs), w)
call sy ' grand sum:' right(commas(grandSum), w+4)
call sy ' grand average:' right(commas(Gavg), w+4)
call sy ' grand OK data:' right(commas(grandOKs), w)
call sy ' grand flagged:' right(commas(grandFlg), w)
if Lflag\==0 then call sy ' longest flagged:' right(commas(Lflag),w) " ending at " Ldate
2013-04-11 01:07:29 -07:00
call sy copies('',60)
2015-11-18 06:14:39 +00:00
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
commas: procedure; parse arg _; n=_'.9'; #=123456789; b=verify(n,#,"M")
e=verify(n,#'0',,verify(n,#"0.",'M'))-4
do j=e to b by -3; _=insert(',',_,j); end /*j*/; return _
/*────────────────────────────────────────────────────────────────────────────*/
sy: say arg(1); call lineout ofid,arg(1); return