Create an HTML table.

 The table body should have at least three rows of three columns.
 Each of these three columns should be labelled "X", "Y", and "Z". 
 An extra column should be added at either the extreme left or the extreme right of the table that has no heading, but is filled with sequential row numbers. 
 The rows of the "X", "Y", and "Z" columns should be filled with random or sequential integers having 4 digits or less. 
 The numbers should be aligned in the same fashion for all columns.


notNumbered     := 0; # possible values for html table row numbering
numberedLeft    := 1; #    "        "    "    "    "    "     "
numberedRight   := 2; #    "        "    "    "    "    "     "
 
alignCentre     := 0; # possible values for html table column alignment
alignLeft       := 1; #    "        "    "    "    "     "        "
alignRight      := 2; #    "        "    "    "    "     "        "
 
# write an html table to a file
writeHtmlTable := proc( fh, t :: table ) is
    local align := "align='";
    case t.columnAlignment
      of alignLeft  then align := align & "left'"
      of alignRight then align := align & "right'"
    else                 align := align &  "center'"
    esac;
    local put       := proc( text    :: string ) is io.write( fh, text & "\n" ) end;
    local thElement := proc( content :: string ) is put( "<th " & align & ">" & content & "</th>" ) end;
    local tdElement := proc( content           ) is put( "<td " & align & ">" & content & "</td>" ) end;
    # table element
    put( "<table"
       & " cellspacing='" & t.cellSpacing & "'"
       &  " colspacing='" & t.colSpacing  & "'"
       &      " border='" & t.border      & "'"
       & ">"
       );
    # table headings
    put( "<tr>" );
    if t.rowNumbering =  numberedLeft then thElement( "" ) fi;
    for col to size t.headings do thElement( t.headings[ col ] ) od;
    if t.rowNumbering = numberedRight then thElement( "" ) fi;
    put( "</tr>" );
    # table rows
    for row to size t.data do
        put( "<tr>" );
        if t.rowNumbering =  numberedLeft then thElement( row & "" ) fi;
        for col to size t.data[ row ] do tdElement( t.data[ row, col ] ) od;
        if t.rowNumbering = numberedRight then thElement( row & "" ) fi;
        put( "</tr>" )
    od;
    # end of table
    put( "</table>" )
end ;
 
# create an html table and print it to standard output
scope
    local t := [];
    t.cellSpacing, t.colSpacing := 0, 0;
    t.border          := 1;
    t.columnAlignment := alignRight;
    t.rowNumbering    := numberedLeft;
    t.headings        := [ "A", "B", "C" ];
    t.data            := [ [ 1001, 1002, 1003 ], [ 21, 22, 23 ], [ 201, 202, 203 ] ];
    writeHtmlTable( io.stdout, t )
epocs