program number(input, output); (* number: add line numbers to a file Dr. Thomas D. Schneider, Ph.D. National Institutes of Health schneidt@mail.nih.gov toms@alum.mit.edu (permanent) http://alum.mit.edu/www/toms (permanent) *) label 1; (* end of program *) const (* begin module version *) version = 1.10; (* of number.p 2010 Mar 01 2010 Mar 01, 1.10 remove blanks before numbers 2001 Sep 13, 1.09 link to cumulative.p 1991 Sep 16, 1.08 origin *) (* end module version *) (* begin module describe.number *) (* name number: add line numbers to a file synopsis number(input: in, output: out) files input: input file output: input file with line numbers at the start description Add line numbers to the input file. examples documentation see also {A slow but effective solution to the technical note below is in:} cumulative.p author Thomas Dana Schneider bugs technical notes Perhaps the program should have an option to define the number of columns for the line numbers. An earlier version computed this. However, to do that requires resetting the file and prevents the program from being used as part of a pipe. The current program simply uses a default value for the number of columns so that number can be used as part of a pipe. See cumulative.p for a solution, not yet implemented. *) (* end module describe.number *) (* begin module halt *) procedure halt; (* stop the program. the procedure performs a goto to the end of the program. you must have a label: label 1; declared, and also the end of the program must have this label: 1: end. examples are in the module libraries. this is the only goto in the delila system. *) begin writeln(output,' program halt.'); goto 1 end; (* end module halt version = 4.11; (@ of prgmod.p 1991 Apr 22 *) (* begin module copyaline *) procedure copyaline(var fin, fout: text); (* copy a line from file fin to file fout *) begin (* copyaline *) while not eoln(fin) do begin fout^ := fin^; put(fout); get(fin) end; readln(fin); writeln(fout); end; (* copyaline *) (* end module copyaline version = 4.11; (@ of prgmod.p 1991 Apr 22 *) (* begin module number.themain *) procedure themain(var fin,fout: text); (* the main procedure of the program *) const { decimals = 5; (* that should be sufficient for most files! *) } decimals = 1; (* No blanks before numbers! *) var count: integer; (* count of the lines produces so far *) { decimals: integer; (* decimal places to express the value of lines in *) lines: integer; (* total lines in the file *) } begin { this is crazy. the extra reset prevents this from being used in pipes. (* determine number of lines in the file *) lines := -1; (* don't count the line that has the eof on it! *) reset(fin); while not eof(fin) do begin lines := lines + 1; readln(fin) end; if lines > 0 then begin (* determine how many decimal places to put out for line numbers *) decimals := trunc(ln(lines)/ln(10))+1; (* copy the file *) reset(fin); for count := 1 to lines do begin write(fout,count:decimals,' '); copyaline(fin,fout) end; end; } count := 0; { reset(fin); } while not eof(fin) do begin count := succ(count); write(fout,count:decimals,' '); copyaline(fin,fout) end; end; (* end module number.themain *) begin themain(input,output); 1: end.