global line, bm_cnt, bm_seq, digit, out_cnt, out_seq, it, pre_it, bm_cnt_mo, bm_seq_mo, mo, da, yr, in, inname, dbg, eof, curr_mo, curr_yr, first record bm(yr,mo,da) procedure main() # &trace := 100000 # 1 procs for bm in one day init() init_1() while process_month_1() do { output_month_1() } finish_1() # 2 procs for conseq days no bm init_2() while process_month_2() do { output_month_2() } finish_2() end procedure init() digit := '1234567890' inname := "/home/dennis/Documents/dennisdarland/html/health2/rest.txt" dbg := open("dbg.txt", "w") write(dbg, "dbg init") return 0 end procedure init_1() first := 1 if not(in := open(inname,"r")) then stop("cannot open " || inname) out_cnt := open("bmcnt.txt","w") write(out_cnt," Days with # of BMs ") write(out_cnt,"Month 0 BM 1 BM 2 BM 3 BM 4 BM 5 BM 6 BM 7 BM 8 BM") bm_cnt := list(12,0) # no of bm in one day - overall bm_cnt_mo := list(12,0) # no of bm in one day - current month eof := 0 last_mo := -1 last_yr := -1 it := get_next_bm() curr_yr := it.yr curr_mo := it.mo return 0 end procedure days_in_mo() local days days := (case curr_mo + 0 of { 1 : 31 2 : 28 3 : 31 4 : 30 5 : 31 6 : 30 7 : 31 8 : 31 9 : 30 10 : 31 11 : 30 12 : 31 default : 100 }) if ((curr_yr % 4) = 0) then days +:= 1 return days end procedure init_2() first := 1 in := open(inname,"r") out_seq := open("bmseq.txt","w") write(out_seq," Consequtive Days with no BMs ") write(out_seq,"Month 1 Day 2 Days 3 Days 4 Days 5 Days 6 Days 7 Days 8 Days") bm_seq_mo := list(12,0) # no conseq days no bm - overall eof := 0 last_mo := -1 last_yr := -1 it := get_next_bm() curr_yr := it.yr curr_mo := it.mo return 0 end procedure finish_1() # write(out_cnt,"Last month may be wrong due to algorithm.") close(in) close(out_cnt) return 0 end procedure finish_2() # write(out_seq,"Last month may be wrong due to algorithm.") close(in) close(out_seq) return 0 end procedure get_next_bm() local no_chk, no_yes, no_no no_chk := 0 no_yes := 0 no_no := 0 while line := read(in) do { no_chk +:= 1 flush(out_cnt) if line ? ((mo := tab(many(digit))) & tab(match("/")) & (da := tab(many(digit))) & tab(match("/")) & (yr := tab(many(digit))) & ( find("BM") | find("bm") )) then { no_yes +:= 1 flush(dbg) flush(out_cnt) return bm(yr,mo,da) } else no_no +:= 1 } eof := 1 fail end procedure process_month_1() local bm_same_day, curr_da bm_same_day := 1 curr_da := it.da curr_mo := it.mo curr_yr := it.yr while (it.mo = curr_mo) & (eof = 0) do { if not (it :=(get_next_bm())) then { break } while (it.mo = curr_mo) & (it.da = curr_da) & (eof = 0) do { bm_same_day +:= 1 # add to bm in one day if not (it := get_next_bm()) then break } bm_cnt_mo[bm_same_day + 1] +:= 1 bm_same_day := 1 curr_da := it.da } if not (it :=(get_next_bm())) then fail return 1 end procedure process_month_2() local days_no_bm, last_da, curr_da days_no_bm := 0 curr_da := it.da curr_mo := it.mo curr_yr := it.yr last_da := 1 while (it.mo = curr_mo) & (eof = 0) do { if not (it :=(get_next_bm())) then { break } while (it.mo = curr_mo) & (it.da = curr_da) & (eof = 0) do { if not (it := get_next_bm()) then break } curr_da := it.da days_no_bm := curr_da - last_da if days_no_bm > 1 then { bm_seq_mo[days_no_bm - 1] +:= 1 } last_da := it.da } if not (it :=(get_next_bm())) then fail return 1 end procedure output_month_1() local i, days_0, days_p if first = 1 then { first := 0 } else { i := 1 days_p := 0 while i <= 12 do { days_p +:= bm_cnt_mo[i] i +:= 1 } days_0 := days_in_mo(curr_mo, curr_yr) - days_p write(out_cnt,left(curr_mo || "/" || curr_yr,7)," ",right(days_0,6)," ",right(bm_cnt_mo[2],6)," ",right(bm_cnt_mo[3],6)," ",right(bm_cnt_mo[4],6)," ",right(bm_cnt_mo[5],6)," ",right(bm_cnt_mo[6],6)," ",right(bm_cnt_mo[7],6)," ",right(bm_cnt_mo[8],6)," ",right(bm_cnt_mo[9],6)) flush(out_cnt) i := 1 while i <= 12 do { bm_cnt_mo[i] := 0 i +:= 1 } first := 0 } end procedure output_month_2() local i if first = 1 then { first := 0 } else { write(out_seq,left(curr_mo || "/" || curr_yr, 7)," ",right(bm_seq_mo[1],6)," ",right(bm_seq_mo[2],6)," ",right(bm_seq_mo[3],6)," ",right(bm_seq_mo[4],6)," ",right(bm_seq_mo[5],6)," ",right(bm_seq_mo[6],6)," ",right(bm_seq_mo[7],6)," ",right(bm_seq_mo[8],6)," ",right(bm_seq_mo[9],6)) flush(out_cnt) flush(out_seq) i := 1 while i <= 12 do { bm_seq_mo[i] := 0 i +:= 1 } } end