Text processing/1: Difference between revisions

Content added Content deleted
m (Fixed link to data file)
(Forth)
Line 411: Line 411:


Output matches that of the Python version.
Output matches that of the Python version.

=={{header|Forth}}==
{{works with|GNU Forth}}
<lang forth>
fvariable day-sum
variable day-n

fvariable total-sum
variable total-n

create cur-date 10 allot
create bad-date 10 allot \ yyyy-mm-dd
variable bad-n

create worst-date 10 allot
variable worst-n

: split ( buf len char -- buf' l2 buf l1 ) \ where buf'[0] = char, l1 = len-l2
>r 2dup r> scan
2swap 2 pick - ;

: next-sample ( buf len -- buf' len' fvalue flag )
#tab split >float drop 1 /string
#tab split snumber? drop >r 1 /string r> ;

: ok? 0> ;

: hour ( buf len -- buf' len' )
next-sample ok? if
day-sum f@ f+ day-sum f!
1 day-n +!
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
bad-n @ 0= if
pad bad-date 10 move
then
1 bad-n +!
then ;

: day ( line len -- )
2dup + #tab swap c! 1+ \ append tab for parsing
#tab split cur-date swap move 1 /string \ skip date
0e day-sum f!
0 day-n !
24 0 do hour loop 2drop
cur-date 10 type ." mean = "
day-sum f@ day-n @ 0 d>f f/ f. cr
day-sum f@ total-sum f@ f+ total-sum f!
day-n @ total-n +! ;

stdin value input

: main
s" input.txt" r/o open-file throw to input
0e total-sum f!
0 total-n !
0 worst-n !
begin pad 512 input read-line throw
while pad swap day
repeat
input close-file throw
." Summary:" cr
worst-n @ if
." Longest interruption: " worst-n @ .
." hours starting " worst-date 10 type cr
then
." Total mean = "
total-sum f@ total-n @ 0 d>f f/ f. cr ;

main bye
</lang>


=={{header|Perl}}==
=={{header|Perl}}==