Re: generic coding problem
- Posted by ChrisBurch2 <crylex at freeuk.co.uk> Aug 11, 2006
- 558 views
Pete Lomax wrote: > > On Tue, 08 Aug 2006 17:16:49 -0700, Chris Burch > <guest at RapidEuphoria.com> wrote: > > >Hi > > > >Thats very good. You type faster than I think. Definitely given me some more > clarifying ideas.</font></i> > btw, that demo is much better with > }}} <eucode> > constant maxwid=79, -- characters across page > split_text_cols=0 -- 0 to stop text splits > ... > colwidths = repeat(0,10+rand(20)) > ... > -- k = rand(3) > for i=1 to length(colfmts) do > k = rand(3) > </eucode> {{{ > > Regards, > Pete > > Hi Pete, this is what I came up with (debugging stops still present)
------------------------------------------------------------------------------------------------------------------ global function printers_reports_table(sequence tbl, sequence tbl_title) --takes a formatted table of strings, and works out the best way of printing it on paper. --Rules -- cells must be pre formatted strings -- cells in each column must all have same width, but -- different columns do not need to be same width -- table must be uniform - eg, 3 * 9, all rows and columns must be the same count --paper rows and columns for printers hard coded here, but may not apply to all printers --seem we can use the enscript slice function for this! ------------------------------------------------------------------------------------------------------------------ sequence orientation integer row, col, max_row, max_col integer landscape_cols, portrait_cols, char_cols, char_count, r_loc sequence print_command, line, line_new integer fp integer col_start, col_end, cur_column, cur_row, cur_char_pos portrait_cols = 79 --actually 80 landscape_cols = 124 --125 --define maximumum row and column numbers max_row = length(tbl) max_col = length(tbl[1]) review_data(tbl) --find the length of characters required to print one row char_count = 0 for i = 1 to max_col do char_count += length(tbl[1][i]) + 1 end for --can only really do this if using enscript to process the text files, so check this if match("enscript", printers[PR_REPORTS]) = 0 then VOID = get_yn_box("No enscript match") return 0 end if --decide on orientation, and create page to print on if char_count <= portrait_cols then orientation = "-R" char_cols = portrait_cols --line = repeat(' ', 80) else orientation = "-r" char_cols = landscape_cols --line = repeat(' ', 125) end if --paper = line --find the position of the "-r" or "-R" in the printer string, --and replace it with the above one --if not present, just add it. r_loc = find("-R", upper(printers[PR_REPORTS])) if r_loc = 0 then print_command = printers[PR_REPORTS] & " " & orientation else print_command = printers[PR_REPORTS][1..r_loc-1] & " " & orientation & printers[PR_REPORTS][r_loc+3..$] end if VOID = get_yn_box(print_command) --first copy as much of the row across onto the paper, --marking where have to stop - next one for next 'sheet' --now create the file to print cur_column = 2 --header count col_start = 2 cur_row = 1 cur_char_pos = 1 line = "" line_new = "" fp = open("report.txt", "w") puts(fp, tbl_title & "\n") for i = 1 to length(tbl_title) do puts(fp, "=") end for puts(fp, "\n\n") while 1 do line_new = "" while 1 do if cur_column = col_start then --put the row title on line_new = tbl[cur_row][1] & " " line = line_new if length(line) > char_cols then VOID = get_yn_box("The row title is wider than the page!") return 0 end if end if --line_new = append(line_new, tbl[cur_row][cur_column] & " ") line_new = line_new & tbl[cur_row][cur_column] & " " if length(line_new) < char_cols then line = line_new cur_column += 1 if cur_column > length(tbl[1]) then --have we gone past the end of the table? exit end if else --line_new is of greater length than the page --write out the line, exit from this loop puts(fp, line & "\n") exit end if end while --advance the row cur_row += 1 --have we gone past the end of the table? if cur_row > length(tbl) then --restart table, with new start of column chunk cur_row = 1 col_start = cur_column + 1 puts(fp, "\n\n") else --otherwise just put it back to the start of the chunk cur_column = col_start end if if col_start > length(tbl[1]) then --have gone past col count exit end if end while close(fp) system_view_file("report.txt") return 1 end function
It took me a lot longer to get here, than you did, but you cleared things in my head, as much as anything else, and its mostly, but not fully, tested yet. Cheers, Chris