Create an HTML table: Difference between revisions

From Rosetta Code
Content added Content deleted
Line 876: Line 876:


\ Now write the program using our new words and it will interpret
\ Now write the program using our new words and it will interpret
<html> cr
<html>
tab <head> </head> cr
tab <head> </head> cr
2 tabs <body> cr
2 tabs <body> cr
Line 892: Line 892:
2 tabs+ </table> cr
2 tabs+ </table> cr
2 tabs </body> cr
2 tabs </body> cr
<html> cr
<html>
</lang>
</lang>
{{out}}
{{out}}

Revision as of 05:31, 15 January 2016

Task
Create an HTML table
You are encouraged to solve this task according to the task description, using any language you may know.

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.

Ada

We define a generic package to output HTML tables:

<lang Ada>with Ada.Strings.Unbounded;

generic

  type Item_Type is private;
  with function To_String(Item: Item_Type) return String is <>;
  with procedure Put(S: String) is <>;
  with procedure Put_Line(Line: String) is <>;

package HTML_Table is

  subtype U_String is Ada.Strings.Unbounded.Unbounded_String;
  function Convert(S: String) return U_String renames
    Ada.Strings.Unbounded.To_Unbounded_String;
  type Item_Array is array(Positive range <>, Positive range <>) of Item_Type;
  type Header_Array is array(Positive range <>) of U_String;
  procedure Print(Items: Item_Array; Column_Heads: Header_Array);

end HTML_Table;</lang>

The implementation of the package:

<lang Ada>package body HTML_Table is

  procedure Print(Items: Item_Array; Column_Heads: Header_Array) is                              
                                                                                                 
     function Blanks(N: Natural) return String is                                                
        -- indention for better readable HTML                                                    
     begin                                                                                       
        if N=0 then
           return "";
        else
           return " " & Blanks(N-1);
        end if;
     end Blanks;
     procedure Print_Row(Row_Number: Positive) is
     begin

Put(Blanks(4) & "" & Positive'Image(Row_Number) & "");

        for I in Items'Range(2) loop

Put("" & To_String(Items(Row_Number, I)) & "");

               end loop;

Put_Line(""); end Print_Row; procedure Print_Body is begin Put_Line(Blanks(2)&"<tbody align = ""right"">"); for I in Items'Range(1) loop Print_Row(I); end loop; Put_Line(Blanks(2)&"</tbody>"); end Print_Body; procedure Print_Header is function To_Str(U: U_String) return String renames Ada.Strings.Unbounded.To_String; begin Put_Line(Blanks(2) & "<thead align = ""right"">"); Put(Blanks(4) & "");

        for I in Column_Heads'Range loop

Put("" & To_Str(Column_Heads(I)) & "");

        end loop;

Put_Line(""); Put_Line(Blanks(2) & "</thead>"); end Print_Header; begin if Items'Length(2) /= Column_Heads'Length then raise Constraint_Error with "number of headers /= number of columns"; end if; Put_Line("

"); Print_Header; Print_Body; Put_Line("

");

  end Print;

end HTML_Table;</lang>

Here is the main program, using an instance of HTML_Table:

<lang Ada>with Ada.Text_IO, Ada.Numerics.Discrete_Random, HTML_Table;

procedure Test_HTML_Table is

  -- define the Item_Type and the random generator
  type  Four_Digits is mod 10_000;
  package Rand is new Ada.Numerics.Discrete_Random(Four_Digits);
  Gen: Rand.Generator;
  -- now we instantiate the generic package HTML_Table
  package T is new HTML_Table
    (Item_Type => Four_Digits,
     To_String => Four_Digits'Image,
     Put       => Ada.Text_IO.Put,
     Put_Line  => Ada.Text_IO.Put_Line);
  -- define the object that will the values that the table contains
  The_Table: T.Item_Array(1 .. 4, 1..3);

begin

  -- fill The_Table with random values
  Rand.Reset(Gen);
  for Rows in The_Table'Range(1) loop
     for Cols in The_Table'Range(2) loop
        The_Table(Rows, Cols) := Rand.Random(Gen);
     end loop;
  end loop;
  -- output The_Table
  T.Print(Items        => The_Table,
          Column_Heads => (T.Convert("X"), T.Convert("Y"), T.Convert("Z")));

end Test_HTML_Table;</lang>


Each time you run the program, you get different random values for the table. Here is a sample output:

<lang html5>

<thead align = "right">
 </thead>
 <tbody align = "right">
 </tbody>
XYZ
1 7255 3014 9436
2 554 3314 8765
3 4832 129 2048
4 31 6897 8265

</lang>

Viewing the output with Lynx:

        X    Y    Z                                                                                  
   1 7255 3014 9436                                                                                  
   2  554 3314 8765                                                                                  
   3 4832  129 2048                                                                                  
   4   31 6897 8265

AutoHotkey

Translation of: C

<lang AutoHotkey>out =

Loop 4

out .= "`r`n" out .= "`r`n
XYZ
" A_Index "" Rand() "" Rand() "" Rand() "

"

MsgBox % clipboard := out

Rand(u=1000){

   Random, n, 1, % u
   return n

}</lang> Output:

XYZ
128955643
2102100971
3582295264
4396762633

AWK

<lang AWK>#!/usr/bin/awk -f BEGIN {

print "

\n <thead align = \"right\">" printf " \n </thead>\n <tbody align = \"right\">\n"
  for (i=1; i<=10; i++) {
printf " \n",i, 10*i, 100*i, 1000*i-1
  }
print " </tbody>\n
XYZ
%2i%5i%5i%5i

\n"

}</lang>

<lang output>

<thead align = "right">
 </thead>
 <tbody align = "right">
 </tbody>
XYZ
1 10 100 999
2 20 200 1999
3 30 300 2999
4 40 400 3999
5 50 500 4999
6 60 600 5999
7 70 700 6999
8 80 800 7999
9 90 900 8999
10 100 1000 9999

</lang>

BBC BASIC

Uses BBC BASIC's *spool command to create a file. <lang bbcbasic> ncols% = 3

     nrows% = 4
     
     *spool temp.htm
     
     PRINT "<html><head></head><body>"

PRINT "

" FOR row% = 0 TO nrows% IF row% = 0 THEN PRINT "" ;
       ELSE
PRINT "" ;
       ENDIF
       FOR col% = 1 TO ncols%
         IF row% = 0 THEN
PRINT "" ;
         ELSE
PRINT "" ;
         ENDIF
       NEXT col%
PRINT "" NEXT row% PRINT "
" ; row% "" CHR$(87 + col%) "" ; RND(9999) "

"

     PRINT "</body></html>"
     
     *spool
     
     SYS "ShellExecute", @hwnd%, 0, "temp.htm", 0, 0, 1

</lang> Output:

<html><head></head><body>
<table border=1 cellpadding=10 cellspacing=0>
<tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>
<tr><th>1</th><td align="right">2791</td><td align="right">8011</td><td align="right">9582</td>
</tr>
<tr><th>2</th><td align="right">6793</td><td align="right">6863</td><td align="right">4790</td>
</tr>
<tr><th>3</th><td align="right">8064</td><td align="right">2626</td><td align="right">3917</td>
</tr>
<tr><th>4</th><td align="right">2660</td><td align="right">8776</td><td align="right">6805</td>
</tr>
</table>
</body></html>

Rendered output:

Bracmat

To make this interesting, the table is created as a Bracmat structure and then converted to XML by a library. This has the advantage that one doesn't have to worry about HTML entities and encoding issues. The contents of the cells are generated by a function that is instantiated with a starting number when the function makeTable is called. Notice the absence of loops. <lang bracmat>( ( makeTable

 =     headTexts
       minRowNr
       maxRowNr
       headCells
       cells
       rows
       Generator
       Table
   .   get$"xmlio.bra"             { A library that converts from Bracmat format to XML or HTML }
     & !arg:(?headTexts.?minRowNr.?maxRowNr.?Generator)
     & ( headCells
       =   cellText
         .     !arg:%?cellText ?arg
             & (th.,!cellText) headCells$!arg
           | 
       )
     & ( cells
       =   cellText cellTexts numberGenerator
         .       !arg
               : (%?cellText ?cellTexts.(=?numberGenerator))
             &   (td.,numberGenerator$)
                 cells$(!cellTexts.'$numberGenerator)
           | 
       )
     & ( rows
       =   headTexts rowNr maxRowNr Generator
         .     !arg:(?headTexts.?rowNr.?maxRowNr.?Generator)
             & !rowNr:~>!maxRowNr
             &   ( tr
                 .   
                   ,   (td.,!rowNr)
                       cells$(!headTexts.!Generator)
                 )
                 \n
                 rows$(!headTexts.!rowNr+1.!maxRowNr.!Generator)
           | 
       )
     &   ( table
         .   
           ,   ( thead
               .   (align.right)
                 , \n (tr.,(th.," ") headCells$!headTexts)
               )
               \n
               ( tbody
               .   (align.right)
                 ,   \n
                       rows
                     $ (!headTexts.!minRowNr.!maxRowNr.!Generator)
               )
         )
       : ?Table
     & str$((XMLIO.convert)$!Table)      { Call library function to create HTML }
 )

& makeTable

 $ ( X Y Z                               { Column headers }
   . 1                                   { Lowest row number }
   . 4                                   { Highest row number }
   .                                     { Function that generates numbers 9, 10, ...}
     ' ( cnt
       .   (cnt=$(new$(==8)))            { This creates an object 'cnt' with scope as a local function variable that survives between calls. }
         & !(cnt.)+1:?(cnt.)
       )
   )

)</lang> Output:

<table><thead align="right">
<tr><th> </th><th>X</th><th>Y</th><th>Z</th></tr></thead>
<tbody align="right">
<tr><td>1</td><td>9</td><td>10</td><td>11</td></tr>
<tr><td>2</td><td>12</td><td>13</td><td>14</td></tr>
<tr><td>3</td><td>15</td><td>16</td><td>17</td></tr>
<tr><td>4</td><td>18</td><td>19</td><td>20</td></tr>
</tbody></table>

C

<lang C>#include <stdio.h>

  1. include <stdlib.h>

int main() { int i;

printf("

" "");

for (i = 0; i < 4; i++) {

printf("", i,

rand() % 10000, rand() % 10000, rand() % 10000); }

printf("
XYZ
%d%d%d%d

");

return 0;

}</lang>output (wiki doesn't like tbody/thead tags):

XYZ
027778869383
1833577936915
266494925386
32723621421

C++

<lang cpp>#include <fstream>

  1. include <boost/array.hpp>
  2. include <string>
  3. include <cstdlib>
  4. include <ctime>
  5. include <sstream>

void makeGap( int gap , std::string & text ) {

  for ( int i = 0 ; i < gap ; i++ ) 
     text.append( " " ) ;

}

int main( ) {

  boost::array<char , 3> chars = { 'X' , 'Y' , 'Z' } ;
  int headgap = 3 ;
  int bodygap = 3 ;
  int tablegap = 6 ;
  int rowgap = 9 ;
  std::string tabletext( "<html>\n" ) ;
  makeGap( headgap , tabletext ) ;
  tabletext += "<head></head>\n" ;
  makeGap( bodygap , tabletext ) ;
  tabletext += "<body>\n" ;
  makeGap( tablegap , tabletext ) ;

tabletext += "

\n" ; makeGap( tablegap + 1 , tabletext ) ; tabletext += "<thead align=\"right\">\n" ; makeGap( tablegap, tabletext ) ; tabletext += "" ;
  for ( int i = 0 ; i < 3 ; i++ ) {
tabletext += "" ; } tabletext += "\n" ; makeGap( tablegap + 1 , tabletext ) ; tabletext += "</thead>" ; makeGap( tablegap + 1 , tabletext ) ; tabletext += "<tbody align=\"right\">\n" ; srand( time( 0 ) ) ; for ( int row = 0 ; row < 5 ; row++ ) { makeGap( rowgap , tabletext ) ; std::ostringstream oss ; tabletext += "" ; } tabletext += "\n" ; } makeGap( tablegap + 1 , tabletext ) ; tabletext += "</tbody>\n" ; makeGap( tablegap , tabletext ) ; tabletext += "
" ;
     tabletext += *(chars.begin( ) + i ) ;
tabletext += "
" ;
     oss << row ;
     tabletext += oss.str( ) ;
     for ( int col = 0 ; col < 3 ; col++ ) {

oss.str( "" ) ; int randnumber = rand( ) % 10000 ; oss << randnumber ;

tabletext += "
" ;

tabletext.append( oss.str( ) ) ;

tabletext += "

\n" ;

  makeGap( bodygap , tabletext ) ;
  tabletext += "</body>\n" ;
  tabletext += "</html>\n" ;
  std::ofstream htmltable( "testtable.html" , std::ios::out | std::ios::trunc ) ;
  htmltable << tabletext ;
  htmltable.close( ) ;
  return 0 ;

}</lang> Output ( of testtable.html ): <LANG html5><html>

  <head></head>
  <body>
<thead align="right">
      </thead>
      <tbody align="right">
      </tbody>
XYZ
012746847352
184665774612
2754316448143
3492857148186
4343674939344
  </body>

</html> </LANG>

C#

<lang csharp>using System; using System.Text;

namespace prog { class MainClass { public static void Main (string[] args) { StringBuilder s = new StringBuilder(); Random rnd = new Random();

s.AppendLine("

"); s.AppendLine("<thead align = \"right\">"); s.Append("");

for(int i=0; i<3; i++)

s.Append(""); s.AppendLine(""); s.AppendLine("</thead>"); s.AppendLine("<tbody align = \"right\">"); for( int i=0; i<3; i++ ) { s.Append("");

for( int j=0; j<3; j++ )

s.Append(""); s.AppendLine(""); } s.AppendLine("</tbody>"); s.AppendLine("
" + "XYZ"[i] + "
"+i+""+rnd.Next(10000)+"

");

Console.WriteLine( s ); } } }</lang>

More modern version

<lang csharp>using System; using System.Text; using System.Xml;

namespace N { public class T { public static void Main() { var headers = new [] { "", "X", "Y", "Z" };

var cols = headers.Select(name => new XElement( "th", name, new XAttribute("text-align", "center") ) );

var rows = Enumerable.Range(0, 4).Select(ri => new XElement( "tr", new XElement("td", ri), Enumerable.Range(0, 4).Select(ci => new XElement( "td", ci, new XAttribute("text-align", "center") ) ) ) );

var xml = new XElement( "table", new XElement( "thead", new XElement("tr", cols), new XElement("tbody", rows) ) );

Console.WriteLine(xml.ToString()); } } } </lang>

Clojure

Using Hiccup (https://github.com/weavejester/hiccup): <lang clojure>(ns rosettacode.html-table

 (:use 'hiccup.core))

(defn [el sq] [:tr (map vector (cycle [el]) sq)]) (html [:table ( :th ["" \X \Y \Z]) (for [n (range 1 4)] (->> #(rand-int 10000) (repeatedly 3) (cons n) ( :td)))])</lang>

CoffeeScript

<lang coffeescript>

  1. This is one of many ways to create a table. CoffeeScript plays nice
  2. with any templating solution built for JavaScript, and of course you
  3. can build tables in the browser using DOM APIs. This approach is just
  4. brute force string manipulation.

table = (header_row, rows) ->

 """
#{header_row} #{rows.join '\n'}
 """
 

tr = (cells) -> "#{cells.join }" th = (s) -> "#{s}" td = (s) -> "#{s}"

rand_n = -> Math.floor Math.random() * 10000

header_cols = [, 'X', 'Y', 'Z'] header_row = tr (th s for s in header_cols)

rows = [] for i in [1..5]

 rows.push tr [
   th(i)
   td rand_n()
   td rand_n()
   td rand_n()
 ]

html = table header_row, rows console.log html </lang>

output:

XYZ
1157574454544
238275029235
31093634381
429724635745
5406367261093

Common Lisp

Using Closure-HTML (https://common-lisp.net/project/closure/closure-html/) installed by the code itself via QuickLisp (http://quicklisp.org/).

<lang Common Lisp> (ql:quickload :closure-html) (use-package :closure-html) (serialize-lhtml

`(table nil

(tr nil ,@(mapcar (lambda (x) (list 'th nil x)) '("" "X" "Y" "Z"))) ,@(loop for i from 1 to 4 collect `(tr nil (th nil ,(format nil "~a" i)) ,@(loop repeat 3 collect `(td nil ,(format nil "~a" (random 10000)))))))

(make-string-sink))

</lang>

Output:

XYZ
1118945604983
2773995975737
3340387675852
480812238177

D

Translation of: C

<lang d>void main() {

 import std.stdio, std.random;

writeln(`

`); writeln("");
 foreach (immutable i; 0 .. 4)
writefln("",
            i, uniform(0,1000), uniform(0,1000), uniform(0,1000));
writeln("
XYZ
%d%d%d%d

");

}</lang> Output:

XYZ
0524739847
113813782
2926580663
3309816750

Delphi

<lang Delphi>program CreateHTMLTable;

{$APPTYPE CONSOLE}

uses SysUtils;

function AddTableRow(aRowNo: Integer): string; begin

Result := Format(' %d%d%d%d',

   [aRowNo, Random(10000), Random(10000), Random(10000)]);

end;

var

 i: Integer;

begin

 Randomize;

Writeln('

'); Writeln(' ');
 for i := 1 to 4 do
   Writeln(AddTableRow(i));
Writeln('
XYZ

');

 Readln;

end.</lang>

Output:

<lang html5>

XYZ
1737126591393
2671050255203
3131615992086
4478566125042

</lang>

EchoLisp

<lang scheme>

styles -

(style 'td "text-align:right") (style 'table "border-spacing: 10px;border:1px solid red") (style 'th "color:blue;")

generic html5 builder
pushes <tag style=..> (proc content) </tag>

(define (emit-tag tag html-proc content ) (if (style tag) (push html (format "<%s style='%a'>" tag (style tag))) (push html (format "<%s>" tag ))) (html-proc content) (push html (format "</%s> " tag )))

html procs
1 tag, 1 proc

(define (h-raw content) (push html (format "%s" content))) (define (h-header headers) (for ((h headers)) (emit-tag 'th h-raw h))) (define (h-row row) (for ((item row)) (emit-tag 'td h-raw item))) (define (h-table table ) (emit-tag 'tr h-header (first table)) ;; add row-num i at head of row (for ((i 1000)(row (rest table))) (emit-tag 'tr h-row (cons i row))))

</lang>

Output:

<lang scheme> (define my-table '(("" X Y Z) (-1111 111 11) (22 -222 2222) (4422 0 42) (33 333 3333) (6666 666 66)))

(stack (define html 'html)) ;; stack of html elements (emit-tag 'table h-table my-table) (string-join (stack->list html) " ") </lang>

X Y Z
0 -1111 111 11
1 22 -222 2222
2 4422 0 42
3 33 333 3333
4 6666 666 66


Erlang

Both external_format/1 and html_table/3 are used by CSV_to_HTML_translation. Keep backwards compatibility when changing or change both. <lang Erlang> -module( create_html_table ).

-export( [external_format/1, html_table/3, task/0] ).

external_format( XML ) -> remove_quoutes( lists:flatten(xmerl:export_simple_content([XML], xmerl_xml)) ).

html_table( Table_options, Headers, Contents ) -> Header = html_table_header( Headers ), Records = [html_table_record(X) || X <- Contents], {table, Table_options, [Header | Records]}.

task() -> Headers = [" ", "X", "Y", "Z"], Contents = [[erlang:integer_to_list(X), random(), random(), random()] || X <- lists:seq(1, 3)], external_format( html_table([{border, 1}, {cellpadding, 10}], Headers, Contents) ).


html_table_header( Items ) -> {tr, [], [{th, [], [X]} || X <- Items]}.

html_table_record( Items ) -> {tr, [], [{td, [], [X]} || X <- Items]}.

random() -> erlang:integer_to_list( random:uniform(1000) ).

remove_quoutes( String ) -> lists:flatten( string:tokens(String, "\"") ). </lang>

Output:
XYZ
16563476
240131059
3579990331

Euphoria

<lang euphoria>puts(1,"

\n") puts(1," \n")

for i = 1 to 3 do

printf(1," ",i)
   for j = 1 to 3 do
printf(1,"",rand(10000))
   end for
puts(1,"\n") end for puts(1,"
XYZ
%d%d

")</lang>

Sample output:

<lang html5>

XYZ
1797873762382
2363219478900
3409815632762

</lang>


F#

<lang fsharp>open System.Xml

type XmlDocument with

   member this.Add element =
       this.AppendChild element
   member this.Element name =
       this.CreateElement(name) :> XmlNode
   member this.Element (name, (attr : (string * string) list)) =
       let node = this.CreateElement(name)
       for a in attr do
           node.SetAttribute (fst a, snd a)
       node
   member this.Element (name, (text : string)) =
       let node = this.CreateElement(name)
       node.AppendChild(this.Text text) |> ignore
       node
   member this.Text text =
       this.CreateTextNode(text)
   end

type XmlNode with

   member this.Add element =
       this.AppendChild element
   end

let head = [""; "X"; "Y"; "Z"]

let xd = new XmlDocument() let html = xd.Add (xd.Element("html")) html.Add(xd.Element("head"))

   .Add(xd.Element("title", "RosettaCode: Create_an_HTML_table"))

let table = html.Add(xd.Element("body")).Add(xd.Element("table", [("style", "text-align:right")])) let tr1 = table.Add(xd.Element("tr")) for th in head do

   tr1.Add(xd.Element("th", th)) |> ignore

for i in [1; 2; 3] do

   let tr = table.Add(xd.Element("tr"))
   tr.Add(xd.Element("th", i.ToString())) |> ignore
   for j in [1; 2; 3] do
       tr.Add(xd.Element("td", ((i-1)*3+j+1000).ToString())) |> ignore

let xw = new XmlTextWriter(System.Console.Out) xw.Formatting <- Formatting.Indented xd.WriteContentTo(xw)</lang>

Output:

(table part only)

X Y Z
1 1001 1002 1003
2 1004 1005 1006
3 1007 1008 1009

Forth

<lang Forth>\ Create an HTML table Rosetta code \ A way to do this in Forth is to extend Forth with new "Words" that output \ the HTML tags directly. These words will then create HTML code when they \ are intepreted by Forth. The extended Forth code looks at lot like HTML :-) \ The biggest difference is that Forth requires 1 space minumum between \ each Word and we specify the code format with CR, TAB TABS and TABS+

\ some helper routines

.input ( delimter -- ) \ parse the input and print it
               PARSE pad place
               pad count type ;
'"' [char] " emit ; \ output a quote char

3 VALUE TAB-WIDTH

\ format control words

tab OUT @ C/L @ < \ *environmental dependency*
                                               \ some systems may not have these variables
            if  tab-width  out @ tab-width MOD - spaces
            else  CR
            then ;
tabs ( n -- ) 0 ?do tab loop ;
tabs+ ( n -- ) tabs space ;


\ Create our HTML extensions to the Forth interpreter

<html> cr ." <html>" cr ; \ starts on newline, outputs a new line automatically
</html> ." </html" ;
<head> ." <head>" ;
</head> ." </head>" ;
<body> ." <body>" ;
</body> ." </body>" ;
."
" ;
." " ;
<thead ." <thead "  ;
align=" ." align=" '"' [char] " .input '"' ." >"; \ parse input to " and print it
." " ;
." " cr ; \ OUTPUTS a new line automatically
." " BL .input ;
." " ;
<thead> ." <thead>" ;
</thead> ." </thead>" ;
." " ;
." " ;
<tbody ." <tbody " ;
</tbody> ." </tbody> " ;

\ Now write the program using our new words and it will interpret

        <html> 

tab <head> </head> cr 2 tabs <body> cr

2 tabs+

cr 3 tabs <thead align=" right" cr 3 tabs+

3 tabs </thead> cr 4 tabs <tbody align=" right" cr

4 tabs+ 4 tabs+ 4 tabs+ 4 tabs+ 4 tabs+

4 tabs </tbody> cr

2 tabs+
ColX ColY ColZ
0 1274 6847 352
1 846 6577 4612
2 7543 1644 8143
3 4928 5714 8186
4 3436 7493 9344

cr

2 tabs </body> cr

        <html>

</lang>

Output:

<lang Forth>

<html>

  <head></head>
     <body>
<thead align="right">
        </thead>
           <tbody align="right">
           </tbody>
ColXColYColZ
012746847352
184665774612
2754316448143
3492857148186
4343674939344
     </body>
<html>

</lang>

Go

html/template is a package in the standard library. <lang go>package main

import (

   "fmt"
   "html/template"
   "os"

)

type row struct {

   X, Y, Z int

}

var tmpl = `

Template:Range $ix, $row := .Template:End
XYZ
Template:$ix Template:$row.X Template:$row.Y Template:$row.Z

`

func main() {

   // create template
   ct := template.Must(template.New("").Parse(tmpl))
   // make up data
   data := make([]row, 4)
   for r := range data {
       data[r] = row{r*3, r*3+1, r*3+2}
   }
   // apply template to data
   if err := ct.Execute(os.Stdout, data); err != nil {
       fmt.Println(err)
   }

}</lang>

Output:

<lang html5>

XYZ
0012
1345
2678
391011

</lang>

Groovy

<lang groovy>import groovy.xml.MarkupBuilder

def createTable(columns, rowCount) {

   def writer = new StringWriter()
   new MarkupBuilder(writer).table(style: 'border:1px solid;text-align:center;') {
       tr {
           th()
           columns.each { title -> th(title)}
       }
       (1..rowCount).each { row ->
           tr {
               td(row)
               columns.each { td((Math.random() * 9999) as int ) }
           }
       }
   }
   writer.toString()

}

println createTable(['X', 'Y', 'Z'], 3)</lang> Output:

X Y Z
1 6106 9898 1584
2 1641 9387 3858
3 8970 4843 681

Haskell

Library: blaze-html

<lang haskell>#!/usr/bin/runhaskell

import Control.Monad (forM_) import System.Random import Data.List as L

import Text.Blaze.Html5 import Text.Blaze.Html.Renderer.Pretty

makeTable :: RandomGen g => [String] -> Int -> g -> Html makeTable headings nRows gen =

 table $ do
   thead $ tr $ forM_ (L.map toHtml headings) th
   tbody $ forM_ (zip [1 .. nRows] $ unfoldr (Just . split) gen)
     (\(x,g) -> tr $ forM_ (take (length headings)
                                 (x:randomRs (1000,9999) g)) (td . toHtml))

main = do

 g <- getStdGen
 putStrLn $ renderHtml $ makeTable ["", "X", "Y", "Z"] 3 g</lang>

Icon and Unicon

<lang Icon>procedure main()

printf("

\n ")

every r := 1 to 4 do {

printf("\n ",r) every 1 to 3 do printf("",?9999) # random 4 digit numbers per cell
  }
printf("\n
XYZ
%d%d

\n")

end

link printf </lang>

printf.icn provides printf

Sample Output:<lang html5>

XYZ
1312932947013
250451695761
370019634183
4169511581240

</lang>

J

We can define:

<lang j>ele=:4 :0

 nm=. x-.LF
 lf=. x-.nm
 ;('<',nm,'>') ,L:0 y ,L:0 '</',nm,'>',lf

)

hTbl=:4 :0

 rows=. 'td' <@ele"1 ":&.>y
 'table' ele ('tr',LF) <@ele ('th' ele x); rows

)</lang>

With these definitions:

<lang j> (;;:'X Y Z') hTbl ":&.>(i.5),.i.5 3

XYZ
0012
1345
2678
391011
4121314

</lang>

Or, if running under jhs:

<lang j>jhtml (;;:'X Y Z') hTbl ":&.>(i.5),.i.5 3</lang>

to display the table inline, as html.

Java

Works with: Java version 1.5+

This example assumes the header row is the first row in the given array and does not add row numbers. They will need to be added by the programmer when constructing the array. <lang java5>public class HTML {

public static String array2HTML(Object[][] array){ StringBuilder html = new StringBuilder(

"

"); for(Object elem:array[0]){ html.append("");

} for(int i = 1; i < array.length; i++){ Object[] row = array[i];

html.append(""); for(Object elem:row){ html.append("");

}

html.append(""); } html.append("
" + elem.toString() + "
" + elem.toString() + "

");

return html.toString(); }

public static void main(String[] args){ Object[][] ints = {{"","X","Y","Z"},{1,1,2,3},{2,4,5,6},{3,7,8,9},{4,10,11,12}}; System.out.println(array2HTML(ints)); } }</lang> Output:

<lang html5>

XYZ
1123
2456
3789
4101112

</lang>

JavaScript

<lang JavaScript><html><head><title>Table maker</title><script type="application/javascript">

// normally, don't do this: at least name it something other than "a" Node.prototype.a = function (e) { this.appendChild(e); return this }

function ce(tag, txt) { var x = document.createElement(tag); x.textContent = (txt === undefined) ?  : txt; return x; }

function make_table(cols, rows) { var tbl = ce('table', ), tr = ce('tr'), th;

tbl.a(tr.a(ce('th')));

var z = 'Z'.charCodeAt(0); for (var l = z - cols + 1; l <= z; l++) tr.a(ce('th', String.fromCharCode(l)));

for (var r = 1; r <= rows; r++) { tbl.a(tr = ce('tr').a(ce('th', r))); for (var c = 0; c < cols; c++) tr.a(ce('td', Math.floor(Math.random() * 10000))); }

document.body .a(ce('style', 'td, th {border: 1px solid #696;' + 'padding:.4ex} td {text-align: right }' + 'table { border-collapse: collapse}')) .a(tbl); } </script></head> <body><script>make_table(5, 4)</script></body></html></lang>


Or, as an alternative to iterative assembly of the HTML string, we could:

  1. Separate the data definition from the HTML rendering, and
  2. generate the HTML from a functional expression which, for ease of legibility and refactoring, visually resembles the structure of the HTML table code itself.

<lang JavaScript>(function (lngCols, lngRows) {

 //range(5, 20) --> [5..20]
 //range('a', 'n') --> ['a'..'n']
 function range(m, n) {
   var blnAlpha = typeof m === 'string',
     iFirst = blnAlpha ? m.charCodeAt(0) : m,
     lstInt = Array.apply(
       null,
       Array((blnAlpha ? n.charCodeAt(0) : n) - iFirst + 1)
     ).map(function (x, i) {
       return iFirst + i;
     });
   return blnAlpha ? lstInt.map(
     function (x) {
       return String.fromCharCode(x);
     }
   ) : lstInt;
 }
 
 // Letter label for first column (last column will be 'Z')
 var strFirstCol = String.fromCharCode('Z'.charCodeAt(0) - (lngCols - 1));
 
 var lstData = [[].concat(range(strFirstCol, 'Z'))].concat(
     range(1, lngRows).map(
       function (row) {
         return [row].concat(
           range(1, lngCols).map(
             function () {
               return Math.floor(
                 Math.random() * 9999
               );
             }
           )
         );
       }
     )
   );
 return [

'

', ' <thead style = "text-align: right;">', ' ' + lstData[0].reduce( function (a, s) { return a + ''; }, '' ) + '', ' </thead>', ' <tbody style = "text-align: right;">', lstData.slice(1).map( function (row) { return ' ' + row.reduce( function (a, s) { return a + ''; }, '' ) + ''; } ).join('\n'), ' </tbody>', '
' + s + '
' + s + '

'

 ].join('\n');

})(3, 4); // (3 columns --> [X..Z]), (4 rows --> [1..4]) </lang>

Output:

<table>
  <thead style = "text-align: right;">
    <tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>
  </thead>
  <tbody style = "text-align: right;">
    <tr><td>1</td><td>9930</td><td>1530</td><td>3070</td></tr>
    <tr><td>2</td><td>5406</td><td>9285</td><td>311</td></tr>
    <tr><td>3</td><td>647</td><td>6491</td><td>3684</td></tr>
    <tr><td>4</td><td>2274</td><td>4709</td><td>1140</td></tr>
  </tbody>
</table>

jq

Works with: jq version 1.4

<lang jq>def html_row:

"", " \(.[] | "\(.)")", ""; def html_header: "<thead align = 'right'>", " \(html_row)", "</thead>"; def html_table(header): "

", " \(header | html_header)", " <tbody align = 'right'>", " \(.[] | html_row)", " </tbody", "

";

  1. Prepend the sequence number

def html_table_with_sequence(header):

 length as $length
 | . as $in
 | [range(0;length) | [.+1] + $in[.]] |  html_table(header);</lang>

Example <lang jq>def data:

 [ [4,5,6],
   [41, 51, 61],
   [401, 501, 601] ];
  1. The first column has no header

data | html_table_with_sequence( ["", "X", "Y", "Z"] )</lang>

Output:

<lang sh>$ jq -r -n -f Create_an_HTML_table.jq

<thead align = 'right'> </thead> <tbody align = 'right'> </tbody
X Y Z
1 4 5 6
2 41 51 61
3 401 501 601

</lang>

Lasso

<lang Lasso>define rand4dig => integer_random(9999, 1)

local(

output = '

\n' ) with el in (' ,X,Y,Z') -> split(',') do { #output -> append('')

}

  1. output -> append('
\n')

loop(5) => {

#output -> append('\n')

loop(3) => {

#output -> append('')

}

#output -> append('\n') }
  1. output -> append('
' + #el + '
' + loop_count + '' + rand4dig + '

\n')

  1. output</lang>

Output:

 XYZ
1599198926754
284413816322
389764175202
4670582258069
575584962577
<table border=2 cellpadding=5  cellspace=0>
<tr><th> </th><th>X</th><th>Y</th><th>Z</th></tr>
<tr>
<td style="font-weight: bold;">1</td><td>5991</td><td>9892</td><td>6754</td></tr>
<tr>
<td style="font-weight: bold;">2</td><td>8441</td><td>3816</td><td>322</td></tr>
<tr>
<td style="font-weight: bold;">3</td><td>8976</td><td>4175</td><td>202</td></tr>
<tr>
<td style="font-weight: bold;">4</td><td>6705</td><td>8225</td><td>8069</td></tr>
<tr>
<td style="font-weight: bold;">5</td><td>7558</td><td>496</td><td>2577</td></tr>
</table>

Liberty BASIC

This creates and saves an html file, then calls web browser to display it.
A time delay is needed to allow this, then the file is deleted. <lang lb>

   nomainwin
   quote$ =chr$( 34)
   html$  ="<html><head></head><body>"

html$ =html$ +"

"
   for i =1 to 4
       d1$ =str$( i)
       d2$ =str$( int( 10000 *rnd( 1)))
       d3$ =str$( int( 10000 *rnd( 1)))
       d4$ =str$( int( 10000 *rnd( 1)))
html$ =html$ +""
   next i
html$ =html$ +"
X Y Z
"; d1$; " " +d2$ +" " +d3$ +" " +d4$ +"

"

   html$ =html$ +"</body></html>"
   open "table.html" for output as #o
       #o html$;
   close #o
   address$ ="table.html"
   run "explorer.exe "; address$
   timer 5000, [on]
   wait
   [on]
   timer 0
   kill "table.html"
   wait

sub quit w$

   close #w$
   end

end sub </lang>


Mathematica / Wolfram Language

<lang Mathematica>x := RandomInteger[10];

Print["

", "\n",""] Scan[Print[""] & , Range[3]] Print["
XYZ
", #, "", x, "", x, "","

"]</lang>

Output :

<table>
<tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>
<tr><td>1</td><td>6</td><td>10</td><td></td></tr>
<tr><td>2</td><td>1</td><td>10</td><td></td></tr>
<tr><td>3</td><td>6</td><td>7</td><td></td></tr>
</table>

MATLAB / Octave

<lang matlab>function htmltable(fid,table,Label)

fprintf(fid,'

\n <thead align = "right">\n'); if nargin<3, fprintf(fid,' \n </thead>\n <tbody align = "right">\n');
  else
fprintf(fid,' '); fprintf(fid,'',Label{:}); fprintf(fid,'\n </thead>\n <tbody align = "right">\n'); end; fprintf(fid,' \n', [1:size(table,1);table']); fprintf(fid,' </tbody>\n
XYZ
%s
%2i%5i%5i%5i

\n');

end</lang>

Output:

>> htmltable(1,ceil(rand(5,3)*10000))
<table>
  <thead align = "right">
    <tr><th></th><td>X</td><td>Y</td><td>Z</td></tr>
  </thead>
  <tbody align = "right">
    <tr><td> 1</td><td> 6639</td><td> 1110</td><td>  296</td></tr>
    <tr><td> 2</td><td> 4864</td><td> 1252</td><td> 8412</td></tr>
    <tr><td> 3</td><td> 3800</td><td> 4556</td><td> 3752</td></tr>
    <tr><td> 4</td><td> 5728</td><td> 6897</td><td> 2157</td></tr>
    <tr><td> 5</td><td> 2272</td><td> 8503</td><td> 7021</td></tr>
  </tbody>
</table>

Modula-2

<lang modula2>MODULE testCGI;

FROM InOut IMPORT WriteCard, WriteLn, WriteString, WriteBf; FROM Arguments IMPORT ArgTable, GetEnv; FROM Strings IMPORT Assign, Length, String;

VAR EnvVars  : ArgTable;

PROCEDURE ReadEnvVar;

VAR Value  : String;

     i              : CARDINAL;

BEGIN

WriteString ('

'); WriteString ('');
  i := 0;
  LOOP
     IF  EnvVars^ [i] = NIL  THEN  EXIT  END;
     Assign (Value, EnvVars^ [i]^);
WriteString ('"); WriteLn; INC (i) END; WriteString("
IndexLengthContent
');
     WriteCard (i, 2);
WriteString ('
');
     WriteCard (Length (Value), 3);
WriteString ('
'); WriteString (Value); WriteString ("

");

END ReadEnvVar;

BEGIN

  GetEnv (EnvVars);
  WriteString ('Content-type:text/html');
  WriteLn;
  WriteLn;
  WriteString ('<html><head>');
  WriteString ('<title>CGI with the Mocka Modula-2 compiler</title>');
  WriteString ('</head><body>');
  WriteLn;

WriteString ('

CGI environment passed along by your browser

');

  ReadEnvVar;
  WriteString ('</body></html>');
  WriteLn;
  WriteBf

END testCGI.</lang>

NetRexx

<lang NetRexx>/* NetRexx */ options replace format comments java crossref symbols nobinary

-- create some test data. Put the data in a Rexx indexed string maxI = 1000 rng = Random() xyz = xyz[0] = 1; xyz[1] = '. X Y Z' -- use a dot to indicate an empty cell loop r_ = 1 for 5

 ra = r_ rng.nextInt(maxI) rng.nextInt(maxI) rng.nextInt(maxI)
 xyz[0] = r_ + 1; xyz[r_ + 1] = ra
 end r_

-- build an HTML string html = htmlHeader() html = html || htmlTable(xyz) html = html || htmlFooter()

-- display HTML at standard output device say html

return

-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- HTML boilerplate header -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ method htmlHeader() public static returns Rexx

 html = '<?xml version="1.0" encoding="UTF-8"?>\n' -
     || '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n' -
     || '<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">\n' -
     || '<head>\n' -
     || '<meta http-equiv="Content-type" content="text/html;charset=UTF-8"/>\n' -
     || '<title>RCreateHTMLTable</title>\n' -
     || '<style type="text/css">\n' -
     || '\n' -
     || '</style>\n' -
     || '</head>\n' -
     || '<body>\n' -

|| '

Rosetta Code – NetRexx Sample Output

\n' - || '

<a href="http://rosettacode.org/wiki/Create_an_HTML_table">Create an HTML table</a>

\n' -

     || 
 return html

-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- HTML footer -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ method htmlFooter() public static returns Rexx

 html = '</body>\n' -
     || '</html>\n' -
     || 
 return html

-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- Create the table -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ method htmlTable(rows, caption = ) public static returns Rexx

html = '

\n' if caption.length() > 0 then do html = html - || '\n' - || '<thead>\n' - || end html = html - || htmlCsvTableRow(rows[1], 'th')'\n' - || '</thead>\n' - || '<tbody>\n' - || loop r_ = 2 to rows[0] html = html - || htmlCsvTableRow(rows[r_]) end r_ html = html - || '</tbody>\n' - || '
'caption'

\n' -

     || 
 return html

-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- Add a row to the table -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ method htmlCsvTableRow(row, tag = 'td', sep = ' ', emptyCell = '.') public static returns Rexx

 if tag = null then tag = 'td'
 row = row.strip('t')
 -- replace HTML special characters with symbol entities
 row = row.changestr('&', '&') -- need to do this one first to avoid double translation
 row = row.changestr('"', '"')
 row = row.changestr("'", ''')
 row = row.changestr('<', '<')
 row = row.changestr('>', '>')
 elmts = 
 elmts[0] = 0
 e_ = 0
 loop while row.length() > 0
   parse row elmt (sep) row
   if elmt == emptyCell then elmt = ' ' -- replace empy cells with non-breaking spaces
   e_ = e_ + 1; elmts[0] = e_; elmts[e_] = elmt
   end

html = '\n' - || loop e_ = 1 to elmts[0] html = html - || '<'tag'>'elmts[e_]'</'tag'>\n' - || end e_ html = html - || '\n' - || return html </lang> Output:

<lang html5><?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US"> <head> <meta http-equiv="Content-type" content="text/html;charset=UTF-8"/> <title>RCreateHTMLTable</title> <style type="text/css"> </style> </head> <body>

Rosetta Code – NetRexx Sample Output

<a href="http://rosettacode.org/wiki/Create_an_HTML_table">Create an HTML table</a>

</thead> <tbody> </tbody>
  X Y Z
1 626 128 386
2 985 568 636
3 639 622 591
4 843 268 436
5 132 526 251

</body> </html> </lang>

Rendered Output:

NewLISP

<lang NewLISP>; file: html-table.lsp

url
http://rosettacode.org/wiki/Create_an_HTML_table
author
oofoe 2012-01-29

(seed (time-of-day)) ; Initialize random number generator.

The "tab" variable tracks the HTML indent. "pad" composes a line
with the appropriate indent and a terminal newline.

(setq tab 0) (define (pad text) (string (dup " " tab) text "\n"))

NewLISP allows almost any character in an identifier, so I can name
my functions after the HTML elements they invoke. This one formats a
single table data cell.

(define ( text) (pad (string "" text "")))

"" will accept either a number of arguments, each one to be
formatted as a table cell, or a single list argument, which is
broken into table cells. For convenience, I format each list item
with the "" function so I can feed it raw lists.

(define () (let ((data (args)) (s (pad ""))) (if (list? (data 0)) (setq data (data 0))) (inc tab) (dolist (el data) (extend s ( el)))

   (dec tab)

(extend s (pad "")) s))

By defining "" as a macro, I ensure that the rows won't be
evaluated until I've got the table started, which preserves the
formatting.
(define-macro (
) (let ((s (pad "
"))) (inc tab) (doargs (row) (extend s (eval row))) (dec tab) (extend s (pad "
"))
   s
   ))
Test
(print ( ( "" "X" "Y" "Z") ( (cons 0 (rand 1000 3))) ( (cons 1 (rand 1000 3))) ( (cons 2 (rand 1000 3))) )) (exit)</lang> Sample output:
<table>
  <tr>
    <td></td>
    <td>X</td>
    <td>Y</td>
    <td>Z</td>
  </tr>
  <tr>
    <td>0</td>
    <td>289</td>
    <td>824</td>
    <td>462</td>
  </tr>
  <tr>
    <td>1</td>
    <td>49</td>
    <td>600</td>
    <td>84</td>
  </tr>
  <tr>
    <td>2</td>
    <td>511</td>
    <td>219</td>
    <td>742</td>
  </tr>
</table>

Table:

X Y Z
0 289 824 462
1 49 600 84
2 511 219 742

Nim

<lang nim>import math, htmlgen randomize()

template randTD(): expr = td($random(1000..9999)) proc randTR(x): auto =

 tr(td($x, style="font-weight: bold"), randTD, randTD, randTD)

echo table(

 tr(th"", th"X", th"Y", th"Z"),
 randTR 1,
 randTR 2,
 randTR 3,
 randTR 4,
 randTR 5)</lang>

Output:

XYZ
1508470598308
2618535498831
3806355611675
4277717696570
5546595084775

Raw output:

<table><tr><th></th><th>X</th><th>Y</th><th>Z</th></tr><tr><td style="font-weight: bold">1</td><td>5084</td><td>7059</td><td>8308</td></tr><tr><td style="font-weight: bold">2</td><td>6185</td><td>3549</td><td>8831</td></tr><tr><td style="font-weight: bold">3</td><td>8063</td><td>5561</td><td>1675</td></tr><tr><td style="font-weight: bold">4</td><td>2777</td><td>1769</td><td>6570</td></tr><tr><td style="font-weight: bold">5</td><td>5465</td><td>9508</td><td>4775</td></tr></table>

Objeck

<lang objeck> class CreateTable {

 function : Main(args : String[]) ~ Nil {
   s := String->New();
    
s->Append(""); s->Append("<thead align = \"right\">"); s->Append("");
   td := "XYZ";
   for(i:=0; i<3; i+=1;) {
s->Append(""); }; s->Append(""); s->Append("</thead>"); s->Append("<tbody align = \"right\">"); for(i:=0; i<3; i+=1;) { s->Append(""); for(j:=0; j<3; j+=1;) { s->Append(""); }; s->Append(""); }; s->Append("</tbody>"); s->Append("
");
     s->Append(td->Get(i));
s->Append("
");
     s->Append(i);
s->Append("
");
       s->Append((Float->Random() * 10000)->As(Int));
s->Append("
");
   s->PrintLine();
 }

} </lang>

OCaml

A simple printf method:

<lang ocaml>let () =

 let buf = Buffer.create 1 in
 let s = Buffer.add_string buf in
 Random.self_init();
s ""; s "<thead align='right'>"; s "";
 List.iter (fun v ->
s ("")
 ) ["X"; "Y"; "Z"];
s ""; s "</thead>"; s "<tbody align='right'>"; for i = 0 to pred 3 do s ("");
     for j = 0 to pred 3 do
s ("");
     done;
s ""; done; s "</tbody>"; s "
" ^ v ^ "
" ^ string_of_int i ^ "" ^ string_of_int (Random.int 1000) ^ "
";
 print_endline (Buffer.contents buf)</lang>

With a dedicated library

Using the library ocaml-xhtml. With this library the validity of the pages is guaranteed by the OCaml type system.

<lang ocaml>open XHTML.M_01_01

let _td s = td [pcdata s] let _th s = th [pcdata s]

let my_table =

 table ~a:[a_border 1]
   (tr
     (_th "") [
     (_th "X");
     (_th "Y");
     (_th "Z")]
   )
 [
   (tr
     (_td "1") [
     (_td "aa");
     (_td "bb");
     (_td "cc")]
   );
   (tr
     (_td "2") [
     (_td "dd");
     (_td "ee");
     (_td "ff")]
   );
 ]

let my_page =

 html
   (head (title (pcdata "My Page")) [])
   (body
     [ h1 [pcdata "My Table"];
       my_table;
     ]
   )

let () =

 pretty_print ~width:80 print_string my_page</lang>

TyXml

The library TyXml contains a module for XHTML that provides the same interface than the previous ocaml-xhtml library.

<lang ocaml>#use "topfind"

  1. require "tyxml"

module X = Xhtml.M_01_01 (* XHTML 1.1 *) module P = Xhtml.P_01_01

let make_table () =

 let td1 = X.td [X.pcdata "1"] in
 let td2 = X.td [X.pcdata "2"] in
 let td3 = X.td [X.pcdata "3"] in
 let my_tr = X.tr td1 [td2; td3] in
 let my_table = X.table my_tr [] in
 (my_table)

let () =

 let my_title = X.title (X.pcdata "My Page") in
 let my_head = X.head my_title [] in
 let my_h1 = X.h1 [X.pcdata "My Table"] in
 let my_table = make_table () in
 let my_body = X.body [my_h1; my_table] in
 let my_html = X.html my_head my_body in
 P.print print_endline my_html;
</lang>

The previous function make_table () produces a simple table, we can replace it by the function below to output a more complex table with thead and tbody:

<lang ocaml>let make_table () =

 let br = X.a_border 1 in
 let th s = X.th [X.pcdata s] in
 let td s = X.td [X.pcdata s] in
 let my_thead = X.thead (X.tr (th "") [th "X"; th "Y"; th "Z"]) [] in
 let my_tr1 = X.tr (td "1") [td "AAA"; td "BBB"; td "CCC"] in
 let my_tr2 = X.tr (td "2") [td "DDD"; td "EEE"; td "FFF"] in
 let my_tr3 = X.tr (td "3") [td "GGG"; td "HHH"; td "III"] in
 let my_tbody = X.tbody my_tr1 [my_tr2; my_tr3] in
 let my_table = X.tablex ~thead:my_thead ~a:[br] my_tbody [] in
 (my_table)</lang>

Oz

As a complete web application, using the "Roads" web programming library. Connect your browser to http://localhost:8080/table after starting the program. <lang oz>declare

[Roads] = {Module.link ['x-ozlib://wmeyer/roads/Roads.ozf']}

fun {Table Session}

  html(
     head(title("Show a table with row and column headings")

style(type:"text/css" css(td 'text-align':center) ))

     body(

{TagFromList table tr(th th("X") th("Y") th("Z")) | {CreateRows 3 5} })) end

fun {CreateRows NumCols NumRows}

  {List.map {List.number 1 NumRows 1}
   fun {$ Row}
      {TagFromList tr

td( {Int.toString Row} ) | {List.map {List.number 1 NumCols 1} fun {$ Col} SequentialNumber = (Row-1)*NumCols + Col in td( {Int.toString SequentialNumber} ) end }}

   end
  }

end

TagFromList = List.toTuple

in

{Roads.registerFunction table Table} {Roads.run}</lang>

PARI/GP

<lang parigp>html(n=3)={

print("\n");
 for(i=1,n,
print1(""); for(j=1,3,print1("")); print("") ); print("
XYZ
"i""random(9999)"
")

};</lang> Output:

<table>
<tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>
<tr><td>1</td><td>6055</td><td>6794</td><td>6034</td></tr>
<tr><td>2</td><td>8930</td><td>1992</td><td>7087</td></tr>
<tr><td>3</td><td>9592</td><td>5836</td><td>7980</td></tr>
</table>

Note also the built-in printtex command, which allows the analogous task to be written as <lang parigp>printtex(matrix(4,4,i,j,if(i==1,if(j==1,"",Strchr(86+j)),if(j==1,i,random(9999)))))</lang>

Pascal

See Delphi

Perl

<lang Perl>my @heading = qw(X Y Z); my $rows = 5;

print '<thead>" } @heading),
       "</thead><tbody>";

for (1 .. $rows) {

print "", (map { "" } @heading), ""; } print "</tbody>
', (map { "$_
$_".int(rand(10000))."
";</lang>

Note that this is a rather inane way (because of the inane task specification) of generating structured document. For serious work, one should use a module such as XML or HTML for well-formedness instead of this ad hoc method.

Perl 6

This is certainly not the only or best way to generate HTML tables using Perl 6; just an example of one possible method.

<lang Perl6>my @header = <  X Y Z>; my $rows = 5;

sub tag ($tag, $string, $param?) { return "<$tag" ~ ($param ?? " $param" !! ) ~ ">$string" ~ "</$tag>" };

my $table = tag('tr', ( tag('th', $_) for @header));

for 1 .. $rows -> $row {

   $table ~=  tag('tr', ( tag('td', $row, 'align="right"')
   ~ (tag('td', (^10000).pick, 'align="right"') for 1..^@header)));  

}

say tag('table', $table, 'cellspacing=4 style="text-align:right; border: 1px solid;"');</lang>

Sample output:

  X Y Z
12179 4778 2717
22160 1592 4348
34511 540 7187
43484 5882 1273
51310 4017 410

Phix

<lang Phix>puts(1,"\n") puts(1," ")

for j=1 to 3 do

printf(1,"",'W'+j)

end for

puts(1,"\n") for i=1 to 3 do printf(1," ",i)
   for j=1 to 3 do
printf(1,"",rand(10000))
   end for
puts(1,"\n") end for puts(1,"
%s
%d%d
")</lang>

Sample output

XYZ
1328764806510
2850019085352
3328766003953

The raw HTML

<lang html5>
XYZ
1328764806510
2850019085352
3328766003953
</lang>

PHP

normal style

<lang PHP><?php /**

* @author Elad Yosifon
* @desc HTML Table - normal style
*/

$cols = array(, 'X', 'Y', 'Z'); $rows = 3;

$html = '<html><body><colgroup>'; foreach($cols as $col) { $html .= '<col style="text-align: left;" />'; } unset($col); $html .= '</colgroup><thead>'; foreach($cols as $col) { $html .= "";

} unset($col);

$html .= '</thead><tbody>'; for($r = 1; $r <= $rows; $r++) { $html .= ''; foreach($cols as $key => $col) { $html .= '';

} unset($col);

$html .= ''; } $html .= '</tbody>
{$col}
' . (($key > 0) ? rand(1, 9999) : $r) . '
</body></html>';

echo $html; </lang>

template engine style

<lang PHP><?php /**

* @author Elad Yosifon 
* @desc HTML Table - template engine style
*/

$cols = array(, 'X', 'Y', 'Z'); $rows = 3; ?> <html> <body>

<colgroup> <?php foreach($cols as $col):?> <col style="text-align: left;" /> <?php endforeach; unset($col) ?> </colgroup> <thead> <?php foreach($cols as $col): ?>

<?php endforeach; unset($col)?>

</thead> <tbody> <?php for($r = 1; $r <= $rows; $r++): ?> <?php foreach($cols as $key => $col): ?>

<?php endforeach; unset($col) ?>

<?php endfor; ?> </tbody>
<?php echo $col?>
<?php echo ($key > 0) ? rand(1, 9999) : $r ?>

</body> </html> </lang>

PicoLisp

<lang PicoLisp>(load "@lib/xhtml.l")

( NIL NIL '(NIL (NIL "X") (NIL "Y") (NIL "Z")) (for N 3 (<row> NIL N 124 456 789) ) )</lang>

PL/I

<lang PL/I> /* Create an HTML table. 6/2011 */

create: procedure options (main);


create_table: procedure (headings, table_contents);

  declare headings(*) character (10) varying;
  declare table_contents(*, *) fixed;
  declare (i, row, col) fixed;
put skip edit ('
') (a); /* Headings. */ put skip edit (' ') (a);
                              /* For an empty column heading */
  do i = 1 to hbound(headings);
put edit (' ' ) (a);
  end;
put edit ('') (a); /* Table contents. */ do row = 1 to hbound(table_contents, 1); /* row number */ put skip edit (' ') (a);
     /* row contents */
     do col = 1 to hbound(table_contents, 2);
put edit (' ' ) (a);
     end;
put edit ('') (a); end; put skip edit ('
', headings(i), '
', row, '', table_contents(row, col), '
' ) (a);

end create_table;

  declare headings (3) character (1) static initial ('X', 'Y', 'Z');
  declare table_contents(3, 3) fixed static initial (
     4, -3, 8,
     7, 2, -6,
     11, 1, 15);
  call create_table (headings, table_contents);

end create;</lang>

PowerShell

<lang PowerShell>

  1. Converts Microsoft .NET Framework objects into HTML that can be displayed in a Web browser.

ConvertTo-Html -inputobject (Get-Date)

  1. Create a PowerShell object using a HashTable

$object = [PSCustomObject]@{

       'A'=(Get-Random -Minimum 0 -Maximum 10);
       'B'=(Get-Random -Minimum 0 -Maximum 10);
       'C'=(Get-Random -Minimum 0 -Maximum 10)}

$object | ConvertTo-Html </lang>

Output:

<lang PowerShell> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>HTML TABLE</title> </head><body>

<colgroup><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/></colgroup>
DisplayHintDateTimeDateDayDayOfWeekDayOfYearHourKindMilliseco ndMinuteMonthSecondTicksTimeOfDayYear
DateTimeSunday, October 26, 2014 2:32:31 PM10/26/2014 12:00:00 AM26Sunday29914< /td>Local56332103163549930751563463814:32:31.56346382014

</body></html>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>HTML TABLE</title> </head><body>

<colgroup><col/><col/><col/></colgroup>
CBA
873

</body></html> </lang>

When raw results are exported to Out-File cmdlet like this: <lang PowerShell> $object | ConvertTo-Html | Out-File -FilePath $env:temp\test.html ; invoke-item $env:temp\test.html </lang>

Sample output

ABC
581

Protium

Opcodes of interest: SDC -- simple document; R!I -- ranged random integer

<lang html><@ SDCLIT> <@ DTBLIT> <@ DTRLITLIT> <@ DTDLITLIT>|[style]background-color:white</@> <@ DTD>X</@> <@ DTD>Y</@> <@ DTD>Z</@>|[style]width:100%; background-color:brown;color:white; text-align:center</@> <@ ITEFORLIT>10| <@ DTRLITCAP> <@ DTDPOSFORLIT>...|[style]background-color:Brown; color:white; text-align:right</@> <@ DTDCAPLIT><@ SAYR!ILI2>1|9999</@>|[style]width:50;text-align:right</@> <@ DTDCAPLIT><@ SAYR!ILI2>1|9999</@>|[style]width:50;text-align:right</@> <@ DTDCAPLIT><@ SAYR!ILI2>1|9999</@>|[style]width:50;text-align:right</@> |[style]background-color:white;color:black</@> </@> </@> |Number Table</@></lang>

Python

<lang python> import random

def rand9999():

   return random.randint(1000, 9999)

def tag(attr=, **kwargs):

   for tag, txt in kwargs.items():
       return '<{tag}{attr}>{txt}</{tag}>'.format(**locals())

if __name__ == '__main__':

   header = tag(tr=.join(tag(th=txt) for txt in ',X,Y,Z'.split(','))) + '\n'
   rows = '\n'.join(tag(tr=.join(tag(' style="font-weight: bold;"', td=i)
                                   + .join(tag(td=rand9999())
                                             for j in range(3))))
                    for i in range(1, 6))
   table = tag(table='\n' + header + rows + '\n')
   print(table)</lang>

Sample output

XYZ
1604046977055
2252554686901
3885137278379
4531343961765
5401359246082

The raw HTML

<lang html5>

XYZ
1604046977055
2252554686901
3885137278379
4531343961765
5401359246082

</lang>

PureBasic

Create an HTML table

<lang PureBasic> Title.s="Create an HTML table"

head.s="" head.s+"<html><head><title>"+Title.s+"</title></head><body>"+chr(13)+chr(10)

tablehead.s

tablehead.s+"

"+chr(13)+chr(10) tablehead.s+""+chr(13)+chr(10)

index=0

tablebody.s="" for row=1 to 4 index+1

tablebody.s+""

for col=1 to 3

tablebody.s+""

next

tablebody.s+""+chr(13)+chr(10) next tablefoot.s="" tablefoot.s+"
XYZ
"+str(index)+""+str(Random(9999,1))+"

"+chr(13)+chr(10)

foot.s="" foot.s+"</body></html>"+chr(13)+chr(10)

FileName.s="Create_an_HTML_table.html" If CreateFile(0,FileName.s)

   WriteString(0,head.s)
   WriteString(0,tablehead.s)
   WriteString(0,tablebody.s)
   WriteString(0,tablefoot.s)
   WriteString(0,foot.s)
   CloseFile(0)
   Else
   Debug "Not WriteString :"+FileName.s

EndIf

RunProgram(FileName.s)

</lang>

Sample output

XYZ
1663858385360
2299538563093
3377644812
4442811003721


The raw HTML

<lang html5> <html><head><title>Create an HTML table</title></head><body>

XYZ
1663858385360
2299538563093
3377644812
4442811003721

</body></html> </lang>

Racket

<lang racket>

  1. lang racket

(require xml)

(define xexpr

 `(html
   (head)
   (body
    (table
     (tr (td) (td "X") (td "Y") (td "Z"))
     ,@(for/list ([i (in-range 1 4)])
         `(tr (td ,(~a i))
              (td ,(~a (random 10000)))
              (td ,(~a (random 10000)))
              (td ,(~a (random 10000)))))))))

(display-xml/content (xexpr->xml xexpr)) </lang>

Rascal

<lang rascal>import IO; import util::Math;

str html(str title, str content) = item("html", item("title", title) + item("body", content)); str item(str op, str content) = "\<<op>\><content>\</<op>\>"; str table(str content) = item("table border=\"0\"", content); str tr(str content) = item("tr", content); str td(str content) = item("td", content);

public str generateTable(int rows){ int i(){return arbInt(10000);}; rows = (tr(td("")+td("X")+td("Y")+td("Z")) | it + tr(td("<x>")+td("<i()>")+td("<i()>")+td("<i()>")) | x <- [1..rows]); writeFile(|file:///location|, html("Rosetta Code Table", table(rows))); return "written"; }</lang>

This will result in a simple html file. For example:

<lang rascal>rascal>generateTable(10) str: "written"</lang>

has as output:

<lang html><html><title>Rosetta Code Table</title><body>

XYZ
125339883208
2315201447
374933791076
424132111848
5116056469
659912431189
774147092854
89184827160
94515726229
1095579709684

</body></html></lang>

which results in:

Retro

Using the casket::html' library which allows creation of HTML using quotes and combinators:

<lang Retro>needs casket::html' with casket::html'

rnd ( -$ ) random 1000 mod toString ;

[ [ [ ] td [ "x" ] td [ "y" ] td [ "z" ] td ] tr

 [ [ "1" ] td [ rnd ] td [ rnd ] td [ rnd ] td ] tr
 [ [ "2" ] td [ rnd ] td [ rnd ] td [ rnd ] td ] tr
 [ [ "3" ] td [ rnd ] td [ rnd ] td [ rnd ] td ] tr
 [ [ "4" ] td [ rnd ] td [ rnd ] td [ rnd ] td ] tr
 [ [ "5" ] td [ rnd ] td [ rnd ] td [ rnd ] td ] tr
 [ [ "6" ] td [ rnd ] td [ rnd ] td [ rnd ] td ] tr

] table</lang>

REXX

The   LINEOUTs   (writes to a file for the various HTML tags) were broken up into separate pieces which
makes reading the file easier and also helps in debugging, but they could've been combined into a
single   LINEOUT   for succinctness. <lang rexx>/*REXX program creates an HTML table of five rows and three columns. */ arg rows .; if rows== then rows=5 /*no ROWS specified? Then use default.*/

     cols = 3                         /*specify three columns for the table. */
  maxRand = 9999                      /*4-digit numbers, allows negative nums*/

headerInfo = 'X Y Z' /*specifify column header information. */

     oFID = 'a_table.html'            /*name of the  output  file.           */
        w = 0                         /*number of writes to the output file. */

call wrt "<html>" call wrt "<head></head>" call wrt "<body>"

call wrt "

" do r=0 to rows /* [↓] handle row 0 as being special.*/ if r==0 then call wrt "" else call wrt ""
     do c=1  for cols                 /* [↓]  for row 0,  add the header info*/
if r==0 then call wrt "" else call wrt ""
     end   /*c*/
 end       /*r*/
call wrt "
" r "" word(headerInfo,c) "" rnd() "

"

call wrt "</body>" call wrt "</html>" say; say w ' records were written to the output file: ' oFID exit /*stick a fork in it, we're all done. */ /*────────────────────────────────────────────────────────────────────────────*/ rnd: return right(random(0,maxRand*2)-maxRand,5) /*REXX doesn't gen neg RANDs.*/ wrt: call lineout oFID,arg(1); say '══►' arg(1); w=w+1; return /*write.*/</lang> output   to the terminal (screen) using the default input of   5   rows:

══► <html>
══► <head></head>
══► <body>
══► <table border=5  cellpadding=20  cellspace=0>
══► <tr><th></th>
══► <th> X </th>
══► <th> Y </th>
══► <th> Z </th>
══► <tr><th> 1 </th>
══► <td align=right> -9529 </td>
══► <td align=right>  2911 </td>
══► <td align=right> -9855 </td>
══► <tr><th> 2 </th>
══► <td align=right>  8487 </td>
══► <td align=right>  1589 </td>
══► <td align=right>  1795 </td>
══► <tr><th> 3 </th>
══► <td align=right> -9795 </td>
══► <td align=right> -8478 </td>
══► <td align=right> -8716 </td>
══► <tr><th> 4 </th>
══► <td align=right> -1737 </td>
══► <td align=right> -6547 </td>
══► <td align=right>  6988 </td>
══► <tr><th> 5 </th>
══► <td align=right> -5600 </td>
══► <td align=right> -2626 </td>
══► <td align=right> -6062 </td>
══► </table>
══► </body>
══► </html>

31  records were written to the output file:  a_table.html

output generated   (written to the   a_table.html   output file):

<html>
<head></head>
<body>
<table border=5  cellpadding=20  cellspace=0>
<tr><th></th>
<th> X </th>
<th> Y </th>
<th> Z </th>
<tr><th> 1 </th>
<td align=right> -1517 </td>
<td align=right>  5513 </td>
<td align=right> -7697 </td>
<tr><th> 2 </th>
<td align=right>  8373 </td>
<td align=right>   142 </td>
<td align=right> -3641 </td>
<tr><th> 3 </th>
<td align=right> -3971 </td>
<td align=right>  -717 </td>
<td align=right>  5390 </td>
<tr><th> 4 </th>
<td align=right>  9727 </td>
<td align=right> -2023 </td>
<td align=right> -2536 </td>
<tr><th> 5 </th>
<td align=right> -6093 </td>
<td align=right> -7179 </td>
<td align=right>   642 </td>
</table>
</body>
</html>

rendered output:

X Y Z
1 -1517 5513 -7697
2 8373 142 -3641
3 -3971 -717 5390
4 9727 -2023 -2536
5 -6093 -7179 642


Ruby

Pure Ruby solution:

<lang ruby> def r

 rand(10000)

end

STDOUT << "".tap do |html|

html << "

" [ ['X', 'Y', 'Z'], [r ,r ,r], [r ,r ,r], [r ,r ,r], [r ,r ,r] ].each_with_index do |row, index| html << "" html << "" html << row.map { |e| ""}.join html << "" end html << "
#{index > 0 ? index : nil }#{e}

"

end </lang>


This creates a plain HTML table, without any CSS to draw borders or to set column widths.

Library: REXML

<lang ruby>def r; rand(10000); end table = [["", "X", "Y", "Z"],

        [ 1,   r,   r,   r],
        [ 2,   r,   r,   r],
        [ 3,   r,   r,   r]]

require 'rexml/document'

xtable = REXML::Element.new("table") table.each do |row|

 xrow = REXML::Element.new("tr", xtable)
 row.each do |cell|
   xcell = REXML::Element.new("td", xrow)
   REXML::Text.new(cell.to_s, false, xcell)
 end

end

formatter = REXML::Formatters::Pretty.new formatter.compact = true formatter.write(xtable, $stdout)</lang>

Output: <lang html5>

X Y Z
1 1358 6488 6434
2 2477 6493 1330
3 240 3038 9849

</lang>

Run BASIC

<lang RunBasic>html "

"

for i = 1 to 5

html "" for j = 1 to 4 if j = 1 then html "" else html ""
 next j
html "" next i html "
RowXYZ
";i;"";i;j;"

"</lang>

Output:

RowXYZ
1121314
2222324
3323334
4424344
5525354

Scala

Scala has built-in support for XML, so you can freely mix XML literals into your Scala source code. This is nice, because instead of using strings to represent XML, you create XML literals that the compiler can understand and verify. This approach lets you easily generate dynamic XML by interweaving Scala code and XML in the same expressions.<lang scala>object TableGenerator extends App {

 val data = List(List("X", "Y", "Z"), List(11, 12, 13), List(12, 22, 23), List(13, 32, 33))
 def generateTable(data: List[List[Any]]) = {
{data.zipWithIndex.map { case (row, rownum) => (if (rownum == 0) Nil else rownum) +: row}. map(row => {row.map(cell => )} )}
         {cell}
 }
 println(generateTable(data))

}</lang>

Scheme

Works with: Guile
Works with: Gauche

<lang scheme>(define table #(

               #("" "X" "Y" "Z")
               #(1 1 2 3)
               #(2 4 5 6)
               #(3 7 8 9)))

(display "

") (do ((r 0 (+ r 1))) ((eq? r (vector-length table))) (display "") (do ((c 0 (+ c 1))) ((eq? c (vector-length (vector-ref table r)))) (if (eq? r 0) (display ""))) (display "")) (display "
"))
               (if (> r 0)
(display "
"))
               (display (vector-ref (vector-ref table r) c))
               (if (eq? r 0)
(display "")) (if (> r 0) (display "

")</lang>

Seed7

<lang seed7>$ include "seed7_05.s7i";

const proc: main is func

 local
   var integer: line is 0;
   var integer: column is 0;
 begin

writeln("

"); writeln("");
   for line range 1 to 3 do
write("");
     for column range 1 to 3 do
write("");
     end for;
writeln(""); end for; writeln("
XYZ
" <& line <& "" <& rand(0, 9999) <& "

")

 end func;</lang>

Output:

<table style="text-align:center; border: 1px solid">
<tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>
<tr><th>1</th><td>9682</td><td>2439</td><td>7698</td></tr>
<tr><th>2</th><td>2958</td><td>4336</td><td>8340</td></tr>
<tr><th>3</th><td>6245</td><td>6544</td><td>457</td></tr>
</table>

Output viewed with a browser:

XYZ
1968224397698
2295843368340
362456544457

Sidef

<lang ruby>class HTML {

   method _attr(Hash h) {
       h.keys.sort.map {|k| %Q' #{k}="#{h{k}}"' }.join()
   }
   method _tag(Hash h, name, value) {
       "<#{name}" + self._attr(h) + '>' + value + "</#{name}>"
   }
   method table(Hash h, *data) { self._tag(h, 'table', data.join()) }
   method table(*data)         { self.table(Hash(), data...) }

}

class Table < HTML {

   method th(Hash h, value) { self._tag(h, 'th', value) }
   method th(value)         { self.th(Hash(), value) }
   method tr(Hash h, *rows) { self._tag(h, 'tr', rows.join()) }
   method tr(*rows)         { self.tr(Hash(), rows...) }
   method td(Hash h, value) { self._tag(h, 'td', value) }
   method td(value)         { self.td(Hash(), value) }

}

var header = %w(  X Y Z); var rows = 5;

var html = HTML.new; var table = Table.new;

say html.table(

   # attributes
   Hash(
       cellspacing => 4,
       style => "text-align:right; border: 1px solid;"
    ),
   # header
   table.tr(header.map{|elem| table.th(elem)}...),
   # rows
   (1..rows).map { |i|
       table.tr(
           table.td(:(align => 'right'), i),
           (header.len - 1).of {
               table.td(Hash(align => 'right'), 10000.rand.int)
           }...
       )
   }...

);</lang>

Output:
<table cellspacing="4" style="text-align:right; border: 1px solid;">
<tr><th> </th><th>X</th><th>Y</th><th>Z</th></tr>
<tr>
  <td align="right">1</td>
  <td align="right">2308</td>
  <td align="right">6448</td>
  <td align="right">2614</td>
</tr>
<tr>
  <td align="right">2</td>
  <td align="right">8830</td>
  <td align="right">553</td>
  <td align="right">5647</td>
</tr>
<tr>
  <td align="right">3</td>
  <td align="right">9636</td>
  <td align="right">5922</td>
  <td align="right">6384</td>
</tr>
<tr>
  <td align="right">4</td>
  <td align="right">9122</td>
  <td align="right">4832</td>
  <td align="right">8813</td>
</tr>
<tr>
  <td align="right">5</td>
  <td align="right">3331</td>
  <td align="right">5528</td>
  <td align="right">701</td>
</tr>
</table>

(tidied afterwards)

Tcl

<lang tcl># Make ourselves a very simple templating lib; just two commands proc TAG {name args} {

   set body [lindex $args end]
   set result "<$name"
   foreach {t v} [lrange $args 0 end-1] {

append result " $t=\"" $v "\""

   }
   append result ">" [string trim [uplevel 1 [list subst $body]]] "</$name>"

} proc FOREACH {var lst str} {

   upvar 1 $var v
   set result {}
   set s [list subst $str]
   foreach v $lst {append result [string trim [uplevel 1 $s]]}
   return $result

}

  1. Build the data we're displaying

set titles {"" "X" "Y" "Z"} set data {} for {set x 0} {$x < 4} {incr x} {

   # Inspired by the Go solution, but with extra arbitrary digits to show 4-char wide values
   lappend data [list \

[expr {$x+1}] [expr {$x*3010}] [expr {$x*3+1298}] [expr {$x*2579+2182}]] }

  1. Write the table to standard out

puts [TAG table border 1 {

   [TAG tr bgcolor #f0f0f0 {

[FOREACH head $titles { [TAG th {$head}] }]

   }]
   [FOREACH row $data {

[TAG tr bgcolor #ffffff { [FOREACH col $row { [TAG td align right {$col}] }] }]

   }]

}]</lang>

TUSCRIPT

<lang tuscript> $$ MODE TUSCRIPT tablefile="table.html" ERROR/STOP CREATE (tablefile,FDF-o,-std-) ACCESS d: WRITE/ERASE/RECORDS/utf8 $tablefile s,tablecontent tablecontent=* WRITE d "<!DOCTYPE html system>" WRITE d "<html><head><title>create html table</title></head>"

WRITE d "<body>

<thead align='right'>" WRITE d ""

WRITE d "</thead>" WRITE d "<tbody align='right'>" LOOP n=1,5 x=RANDOM_NUMBERS (1,9999,1) y=RANDOM_NUMBERS (1,9999,1) z=RANDOM_NUMBERS (1,9999,1)

WRITE d ""

ENDLOOP

WRITE d "</tbody>
 xyz
{n}{x}{y}{z}

</body></html>"

ENDACCESS d BROWSE $tablefile </lang> Output:

<!DOCTYPE html system>
<html><head><title>create html table</title></head>
<body><table><thead align='right'>
<tr><th> </th><th>x</th><th>y</th><th>z</th></tr>
</thead>
<tbody align='right'>
<tr><td>1</td><td>268</td><td>2409</td><td>8627</td></tr>
<tr><td>2</td><td>2095</td><td>1455</td><td>269</td></tr>

<tr><td>3</td><td>3763</td><td>9225</td><td>1957</td></tr>
<tr><td>4</td><td>1304</td><td>9434</td><td>2208</td></tr>
<tr><td>5</td><td>3547</td><td>4051</td><td>4859</td></tr>
</tbody></table></body></html>

UNIX Shell

Works with: ksh93

<lang bash>function emit_table {

   nameref d=$1
   typeset -i idx=0

echo "

" emit_row th "" "${d[idx++][@]}" for (( ; idx<${#d[@]}; idx++ )); do emit_row td $idx "${d[idx][@]}" done echo "

"

}

function emit_row {

   typeset tag=$1; shift

typeset row="" for elem; do row+=$(printf "<%s>%s</%s>" "$tag" "$elem" "${tag## *}") done row+="" echo "$row" } function addrow { nameref d=$1 typeset n=${#d[@]} typeset -i i for ((i=0; i<$2; i++)); do d[n][i]=$(( $RANDOM % 10000 )) done } n=3 typeset -a data data[0]=("X" "Y" "Z") for i in {1..4}; do addrow data $n done emit_table data</lang>

Output:
<table>
<tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>
<tr><td>1</td><td>4988</td><td>1296</td><td>475</td></tr>
<tr><td>2</td><td>6823</td><td>533</td><td>7530</td></tr>
<tr><td>3</td><td>9975</td><td>257</td><td>6030</td></tr>
<tr><td>4</td><td>475</td><td>1720</td><td>9629</td></tr>
</table>
XYZ
149881296475
268235337530
399752576030
447517209629

VBA

<lang VBA> Public Sub BuildHTMLTable() 'simple HTML table, represented as a string matrix "cells" Const nRows = 6 Const nCols = 4 Dim cells(1 To nRows, 1 To nCols) As String Dim HTML As String 'the HTML table Dim temp As String Dim attr As String

' fill table ' first row with titles cells(1, 1) = "" cells(1, 2) = "X" cells(1, 3) = "Y" cells(1, 4) = "Z" 'next rows with index & random numbers For i = 2 To nRows

 cells(i, 1) = Format$(i - 1)
 For j = 2 To nCols
   cells(i, j) = Format$(Int(Rnd() * 10000))
 Next j

Next i

'build the HTML HTML = "" For i = 1 To nRows

 temp = ""
 'first row as header row
 If i = 1 Then attr = "th" Else attr = "td"
 For j = 1 To nCols
   temp = temp & HTMLWrap(cells(i, j), attr)
 Next j
 HTML = HTML & HTMLWrap(temp, "tr")

Next i HTML = HTMLWrap(HTML, "table", "style=""text-align:center; border: 1px solid""") Debug.Print HTML End Sub

Public Function HTMLWrap(s As String, tag As String, ParamArray attributes()) As String

 'returns string s wrapped in HTML tag with optional "attribute=value" strings
 'ex.: HTMLWrap("Link text", "a", "href=""http://www.somesite.org""")
 'returns: <a href="http://www.somesite.org">Link text</a>
 
 Dim sOpenTag As String
 Dim sClosingTag As String
 sOpenTag = "<" & tag
 For Each attr In attributes
   sOpenTag = sOpenTag & " " & attr
 Next
 sOpenTag = sOpenTag & ">"
 sClosingTag = "</" & tag & ">"
 HTMLWrap = sOpenTag & s & sClosingTag

End Function </lang>

Subroutine BuildHTMLTable builds the HTML code as one big string. Sample output of call to BuildHMTLTable:

<table style="text-align:center; border: 1px solid"><tr><th></th><th>X</th><th>Y</th><th>Z</th></tr><tr><td>1</td><td>1906</td><td>6840</td><td>7474</td></tr><tr><td>2</td><td>6139</td><td>7821</td><td>1617</td></tr><tr><td>3</td><td>8077</td><td>2026</td><td>9567</td></tr><tr><td>4</td><td>658</td><td>615</td><td>7931</td></tr><tr><td>5</td><td>3796</td><td>4635</td><td>1195</td></tr></table>

which corresponds to:

XYZ
1190668407474
2613978211617
3807720269567
46586157931
5379646351195

VBScript

<lang vb> Set objFSO = CreateObject("Scripting.FileSystemObject")

'Open the input csv file for reading. The file is in the same folder as the script. Set objInFile = objFSO.OpenTextFile(objFSO.GetParentFolderName(WScript.ScriptFullName) &_ "\in.csv",1)

'Create the output html file. Set objOutHTML = objFSO.OpenTextFile(objFSO.GetParentFolderName(WScript.ScriptFullName) &_ "\out.html",2,True)

'Write the html opening tags. objOutHTML.Write "<html><head></head><body>" & vbCrLf

'Declare table properties.

objOutHTML.Write "

" & vbCrLf 'Write column headers. objOutHTML.Write "" & vbCrLf

'Go through each line of the input csv file and write to the html output file. n = 1 Do Until objInFile.AtEndOfStream line = objInFile.ReadLine If Len(line) > 0 Then token = Split(line,",")

objOutHTML.Write ""

For i = 0 To UBound(token)

objOutHTML.Write ""

Next

objOutHTML.Write "" & vbCrLf End If n = n + 1 Loop 'Write the html closing tags. objOutHTML.Write "
XYZ
" & n & "" & token(i) & "

</body></html>"

objInFile.Close objOutHTML.Close Set objFSO = Nothing </lang>

Input:
8,1490,9436
555,3300,9766
4982,456,9076
3672,6667,6578
Output:
<html><head></head><body>
<table border=1 cellpadding=10 cellspacing=0>
<tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>
<tr align="right"><td>1</td><td>8</td><td>1490</td><td>9436</td></tr>
<tr align="right"><td>2</td><td>555</td><td>3300</td><td>9766</td></tr>
<tr align="right"><td>3</td><td>4982</td><td>456</td><td>9076</td></tr>
<tr align="right"><td>4</td><td>3672</td><td>6667</td><td>6578</td></tr>
</table></body></html>

Corresponds to:

XYZ
1814909436
255533009766
349824569076
4367266676578

XSLT

<lang xml><?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

   <xsl:output method="html" version="4.01" indent="yes"/>
   <xsl:param name="column-count" select="3"/>
   <xsl:template match="/">
       <html>
           <head>
               <title>Rosetta Code: Create an HTML table (XSLT)</title>
           </head>
           <body>
               <xsl:apply-templates/>
           </body>
       </html>
       <xsl:variable name="values" select="/*/*"/>
   </xsl:template>


   <xsl:template match="/numbers">
<xsl:apply-templates select="number[position() mod $column-count = 1]"/>
X Y Z
   </xsl:template>
   <xsl:template match="number">
               <xsl:value-of select="position()"/>

<xsl:for-each select=". | following-sibling::number[position() < $column-count]">

                   <xsl:value-of select="."/>

</xsl:for-each> </xsl:template> </xsl:stylesheet></lang> XSLT does not have a standard PRNG facility, so a list of numbers is given as input. Sample input: <lang xml><?xml version="1.0" encoding="UTF-8"?> <numbers> <number>1578</number> <number>4828</number> <number>1154</number> <number>4950</number> <number>6497</number> <number>2355</number> <number>9341</number> <number>1927</number> <number>8720</number> <number>4490</number> <number>1218</number> <number>6675</number> <number>8181</number> <number>1403</number> <number>4637</number> </numbers></lang> Sample output: <lang html><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Rosetta Code: Create an HTML table (XSLT)</title> </head> <body>

X Y Z
1 1578 4828 1154
2 4950 6497 2355
3 9341 1927 8720
4 4490 1218 6675
5 8181 1403 4637

</body>

</html></lang>

Rendered table from sample output:

X Y Z
1 1578 4828 1154
2 4950 6497 2355
3 9341 1927 8720
4 4490 1218 6675
5 8181 1403 4637

zkl

<lang zkl>table:=0'|

| "";

table=Sink(table); foreach n in ([1..3]){

table.write("\n "); foreach n in (3){ table.write(""); } table.write(""); } table.write("\n
XYZ
",n,"",(0).random(10000),"

\n").close().print();</lang>

Output:
XYZ
1934263941339
2796345141342
3193782887358
Output:
<table style="text-align:center; border: 1px solid"><th></th><th>X</th><th>Y</th><th>Z</th><tr>
   <tr><th>1</th><td>9342</td><td>6394</td><td>1339</td></tr>
   <tr><th>2</th><td>7963</td><td>4514</td><td>1342</td></tr>
   <tr><th>3</th><td>1937</td><td>8288</td><td>7358</td></tr>
</table>