Anonymous user
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
variable bad-n
create worst-date
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 -- )
: add-day
day-sum f@ total-sum f@ f+ total-sum f!▼
day-n @ total-n +! ;▼
: add-bad-run
: check-worst-run
0 bad-n ! ;
: hour ( buf len -- buf' len' )
next-sample ok? if
add-sample
▲ day-sum f@ f+ day-sum f!
▲ bad-n @ if
▲ bad-n @ worst-n @ > if
▲ bad-n @ worst-n !
▲ bad-date worst-date 10 move
▲ 0 bad-n !
▲ then
else
fdrop
add-bad-
▲ 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
day-sum f@ day-n @
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
worst-n @ if
." Longest interruption: " worst-n @ .
." hours starting " worst-date
then
." Total mean = "
total-sum f@ total-n @
main bye
|