Jump to content

Text processing/1: Difference between revisions

→‎{{header|Forth}}: more factored
(Forth)
(→‎{{header|Forth}}: more factored)
Line 415:
{{works with|GNU Forth}}
<lang forth>
\ data munging
 
\ 1991-03-30[\t10.000\t[-]1]*24
 
\ 1. mean of valid (flag > 0) values per day and overall
\ 2. length of longest run of invalid values, and when it happened
 
fvariable day-sum
variable day-n
Line 421 ⟶ 428:
variable total-n
 
10 constant date-size \ yyyy-mm-dd
create cur-date 10 allot
create badcur-date 10date-size allot \ yyyy-mm-dd
 
create curbad-date 10date-size allot
variable bad-n
 
create worst-date 10date-size allot
variable worst-n
 
Line 433 ⟶ 442:
 
: next-sample ( buf len -- buf' len' fvalue flag )
#tab split >float drop 1 /string
#tab split snumber? drop >r 1 /string r> ;
 
: ok? 0> ;
 
: add-sample ( value -- )
day-sum f@ f+ day-sum f!
1 badday-n +! ;
: add-day
day-sum f@ total-sum f@ f+ total-sum f!
day-n @ total-n +! ;
 
: add-bad-run
bad-n @ 0= if
padcur-date bad-date 10date-size move
then
01 bad-n +! ;
 
: check-worst-run
bad-n @ worst-n @ > if
bad-n @ worst-n !
bad-date worst-date 10date-size move
then
0 bad-n ! ;
 
: hour ( buf len -- buf' len' )
next-sample ok? if
add-sample
day-sum f@ f+ day-sum f!
1 daycheck-n +!worst-run
bad-n @ if
bad-n @ worst-n @ > if
bad-n @ worst-n !
bad-date worst-date 10 move
then
0 bad-n !
then
else
fdrop
add-bad-n @ 0= ifrun
pad bad-date 10 move
then
1 bad-n +!
then ;
 
: .mean ( sum count -- ) 0 d>f f/ f. ;
 
: day ( line len -- )
Line 463 ⟶ 485:
0 day-n !
24 0 do hour loop 2drop
cur-date 10date-size type ." mean = "
day-sum f@ day-n @ 0 d>f f/ f.mean cr
add-day ;
day-sum f@ total-sum f@ f+ total-sum f!
day-n @ total-n +! ;
 
stdin value input
Line 479 ⟶ 500:
repeat
input close-file throw
." Summary:" cr
worst-n @ if
." Longest interruption: " worst-n @ .
." hours starting " worst-date 10date-size type cr
then
." Total mean = "
total-sum f@ total-n @ 0 d>f f/ f.mean cr ;
 
main bye
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.