This commit is contained in:
Ingy döt Net 2013-04-10 16:57:12 -07:00
parent 518da4a923
commit 764da6cbbb
6144 changed files with 83610 additions and 11 deletions

View file

@ -0,0 +1,6 @@
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.

View file

@ -0,0 +1,3 @@
---
category:
- HTML

View file

@ -0,0 +1,13 @@
#!/usr/bin/awk -f
BEGIN {
print "<table>\n <thead align = \"right\">";
printf " <tr><th></th><td>X</td><td>Y</td><td>Z</td></tr>\n </thead>\n <tbody align = \"right\">\n";
};
{
printf " <tr><td>%2i</td><td>%5i</td><td>%5i</td><td>%5i</td></tr>\n",NR,$1,$2,$3;
};
END {
print " </tbody>\n</table>\n";
};

View file

@ -0,0 +1,19 @@
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;

View file

@ -0,0 +1,56 @@
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) & "<tr><td>" & Positive'Image(Row_Number) & "</td>");
for I in Items'Range(2) loop
Put("<td>" & To_String(Items(Row_Number, I)) & "</td>");
end loop;
Put_Line("</tr>");
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) & "<tr><th></th>");
for I in Column_Heads'Range loop
Put("<td>" & To_Str(Column_Heads(I)) & "</td>");
end loop;
Put_Line("</tr>");
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("<table>");
Print_Header;
Print_Body;
Put_Line("</table>");
end Print;
end HTML_Table;

View file

@ -0,0 +1,32 @@
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;

View file

@ -0,0 +1,11 @@
<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> 7255</td><td> 3014</td><td> 9436</td></tr>
<tr><td> 2</td><td> 554</td><td> 3314</td><td> 8765</td></tr>
<tr><td> 3</td><td> 4832</td><td> 129</td><td> 2048</td></tr>
<tr><td> 4</td><td> 31</td><td> 6897</td><td> 8265</td></tr>
</tbody>
</table>

View file

@ -0,0 +1,10 @@
out = <table style="text-align:center; border: 1px solid"><th></th><th>X</th><th>Y</th><th>Z</th><tr>
Loop 4
out .= "`r`n<tr><th>" A_Index "</th><td>" Rand() "</td><td>" Rand() "</td><td>" Rand() "</tr>"
out .= "`r`n</table>"
MsgBox % clipboard := out
Rand(u=1000){
Random, n, 1, % u
return n
}

View file

@ -0,0 +1,30 @@
ncols% = 3
nrows% = 4
*spool temp.htm
PRINT "<html><head></head><body>"
PRINT "<table border=1 cellpadding=10 cellspacing=0>"
FOR row% = 0 TO nrows%
IF row% = 0 THEN
PRINT "<tr><th></th>" ;
ELSE
PRINT "<tr><th>" ; row% "</th>" ;
ENDIF
FOR col% = 1 TO ncols%
IF row% = 0 THEN
PRINT "<th>" CHR$(87 + col%) "</th>" ;
ELSE
PRINT "<td align=""right"">" ; RND(9999) "</td>" ;
ENDIF
NEXT col%
PRINT "</tr>"
NEXT row%
PRINT "</table>"
PRINT "</body></html>"
*spool
SYS "ShellExecute", @hwnd%, 0, "temp.htm", 0, 0, 1

View file

@ -0,0 +1,68 @@
#include <fstream>
#include <boost/array.hpp>
#include <string>
#include <cstdlib>
#include <ctime>
#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 += "<table>\n" ;
makeGap( tablegap + 1 , tabletext ) ;
tabletext += "<thead align=\"right\">\n" ;
makeGap( tablegap, tabletext ) ;
tabletext += "<tr><th></th>" ;
for ( int i = 0 ; i < 3 ; i++ ) {
tabletext += "<td>" ;
tabletext += *(chars.begin( ) + i ) ;
tabletext += "</td>" ;
}
tabletext += "</tr>\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 += "<tr><td>" ;
oss << row ;
tabletext += oss.str( ) ;
for ( int col = 0 ; col < 3 ; col++ ) {
oss.str( "" ) ;
int randnumber = rand( ) % 10000 ;
oss << randnumber ;
tabletext += "<td>" ;
tabletext.append( oss.str( ) ) ;
tabletext += "</td>" ;
}
tabletext += "</tr>\n" ;
}
makeGap( tablegap + 1 , tabletext ) ;
tabletext += "</tbody>\n" ;
makeGap( tablegap , tabletext ) ;
tabletext += "</table>\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 ;
}

View file

@ -0,0 +1,34 @@
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("<table>");
s.AppendLine("<thead align = \"right\">");
s.Append("<tr><th></th>");
for(int i=0; i<3; i++)
s.Append("<td>" + "XYZ"[i] + "</td>");
s.AppendLine("</tr>");
s.AppendLine("</thead>");
s.AppendLine("<tbody align = \"right\">");
for( int i=0; i<3; i++ )
{
s.Append("<tr><td>"+i+"</td>");
for( int j=0; j<3; j++ )
s.Append("<td>"+rnd.Next(10000)+"</td>");
s.AppendLine("</tr>");
}
s.AppendLine("</tbody>");
s.AppendLine("</table>");
Console.WriteLine( s );
}
}
}

View file

@ -0,0 +1,16 @@
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
printf("<table style=\"text-align:center; border: 1px solid\"><th></th>"
"<th>X</th><th>Y</th><th>Z</th>");
for (i = 0; i < 4; i++) {
printf("<tr><th>%d</th><td>%d</td><td>%d</td><td>%d</td></tr>", i,
rand() % 10000, rand() % 10000, rand() % 10000);
}
printf("</table>");
return 0;
}

View file

@ -0,0 +1,32 @@
# This is one of many ways to create a table. CoffeeScript plays nice
# with any templating solution built for JavaScript, and of course you
# can build tables in the browser using DOM APIs. This approach is just
# brute force string manipulation.
table = (header_row, rows) ->
"""
<table>
#{header_row}
#{rows.join '\n'}
</table>
"""
tr = (cells) -> "<tr>#{cells.join ''}</tr>"
th = (s) -> "<th align='right'>#{s}</th>"
td = (s) -> "<td align='right'>#{s}</td>"
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

View file

@ -0,0 +1,10 @@
import std.stdio, std.random;
void main() {
writeln(`<table style="text-align:center; border: 1px solid">`);
writeln("<th></th><th>X</th><th>Y</th><th>Z</th>");
foreach (i; 0 .. 4)
writefln("<tr><th>%d</th><td>%d</td><td>%d</td><td>%d</td></tr>",
i, uniform(0,1000), uniform(0,1000), uniform(0,1000));
writeln("</table>");
}

View file

@ -0,0 +1,23 @@
program CreateHTMLTable;
{$APPTYPE CONSOLE}
uses SysUtils;
function AddTableRow(aRowNo: Integer): string;
begin
Result := Format(' <tr><td>%d</td><td>%d</td><td>%d</td><td>%d</td></tr>',
[aRowNo, Random(10000), Random(10000), Random(10000)]);
end;
var
i: Integer;
begin
Randomize;
Writeln('<table>');
Writeln(' <tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>');
for i := 1 to 4 do
Writeln(AddTableRow(i));
Writeln('</table>');
Readln;
end.

View file

@ -0,0 +1,7 @@
<table>
<tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>
<tr><td>1</td><td>7371</td><td>2659</td><td>1393</td></tr>
<tr><td>2</td><td>6710</td><td>5025</td><td>5203</td></tr>
<tr><td>3</td><td>1316</td><td>1599</td><td>2086</td></tr>
<tr><td>4</td><td>4785</td><td>6612</td><td>5042</td></tr>
</table>

View file

@ -0,0 +1,10 @@
puts(1,"<table>\n")
puts(1," <tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>\n")
for i = 1 to 3 do
printf(1," <tr><td>%d</td>",i)
for j = 1 to 3 do
printf(1,"<td>%d</td>",rand(10000))
end for
puts(1,"</tr>\n")
end for
puts(1,"</table>")

View file

@ -0,0 +1,6 @@
<table>
<tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>
<tr><td>1</td><td>7978</td><td>7376</td><td>2382</td></tr>
<tr><td>2</td><td>3632</td><td>1947</td><td>8900</td></tr>
<tr><td>3</td><td>4098</td><td>1563</td><td>2762</td></tr>
</table>

View file

@ -0,0 +1,36 @@
package main
import (
"fmt"
"html/template"
"os"
)
type row struct {
X, Y, Z int
}
var tmpl = `<table>
<tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>
{{range $ix, $row := .}} <tr><td>{{$ix}}</td>` +
`<td>{{$row.X}}</td>` +
`<td>{{$row.Y}}</td>` +
`<td>{{$row.Z}}</td></tr>
{{end}}</table>
`
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)
}
}

View file

@ -0,0 +1,7 @@
<table>
<tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>
<tr><td>0</td><td>0</td><td>1</td><td>2</td></tr>
<tr><td>1</td><td>3</td><td>4</td><td>5</td></tr>
<tr><td>2</td><td>6</td><td>7</td><td>8</td></tr>
<tr><td>3</td><td>9</td><td>10</td><td>11</td></tr>
</table>

View file

@ -0,0 +1,20 @@
#!/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

View file

@ -0,0 +1,25 @@
public class HTML {
public static String array2HTML(Object[][] array){
StringBuilder html = new StringBuilder(
"<table>");
for(Object elem:array[0]){
html.append("<th>" + elem.toString() + "</th>");
}
for(int i = 1; i < array.length; i++){
Object[] row = array[i];
html.append("<tr>");
for(Object elem:row){
html.append("<td>" + elem.toString() + "</td>");
}
html.append("</tr>");
}
html.append("</table>");
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));
}
}

View file

@ -0,0 +1 @@
<table><th></th><th>X</th><th>Y</th><th>Z</th><tr><td>1</td><td>1</td><td>2</td><td>3</td></tr><tr><td>2</td><td>4</td><td>5</td><td>6</td></tr><tr><td>3</td><td>7</td><td>8</td><td>9</td></tr><tr><td>4</td><td>10</td><td>11</td><td>12</td></tr></table>

View file

@ -0,0 +1,35 @@
<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>

View file

@ -0,0 +1,12 @@
function htmltable(fid,table,Label)
fprintf(fid,'<table>\n <thead align = "right">\n');
if nargin<3,
fprintf(fid,' <tr><th></th><td>X</td><td>Y</td><td>Z</td></tr>\n </thead>\n <tbody align = "right">\n');
else
fprintf(fid,' <tr><th></th>');
fprintf(fid,'<td>%s</td>',Label{:});
fprintf(fid,'</tr>\n </thead>\n <tbody align = "right">\n');
end;
fprintf(fid,' <tr><td>%2i</td><td>%5i</td><td>%5i</td><td>%5i</td></tr>\n', [1:size(table,1);table']);
fprintf(fid,' </tbody>\n</table>\n');
end

View file

@ -0,0 +1,36 @@
<?php
/**
* @author Elad Yosifon <elad.yosifon@gmail.com>
* @desc HTML Table - native style
*/
$cols = array('', 'X', 'Y', 'Z');
$rows = 3;
$html = '<html><body><table><colgroup>';
foreach($cols as $col)
{
$html .= '<col style="text-align: left;" />';
}
unset($col);
$html .= '</colgroup><thead><tr>';
foreach($cols as $col)
{
$html .= "<td>{$col}</td>";
}
unset($col);
$html .= '</tr></thead><tbody>';
for($r = 1; $r <= $rows; $r++)
{
$html .= '<tr>';
foreach($cols as $key => $col)
{
$html .= '<td>' . (($key > 0) ? rand(1, 9999) : $r) . '</td>';
}
unset($col);
$html .= '</tr>';
}
$html .= '</tbody></table></body></html>';
echo $html;

View file

@ -0,0 +1,35 @@
<?php
/**
* @author Elad Yosifon <elad.yosifon@gmail.com>
* @desc HTML Table - spaghetti-code style
*/
$cols = array('', 'X', 'Y', 'Z');
$rows = 3;
?>
<html>
<body>
<table>
<colgroup>
<?php foreach($cols as $col):?>
<col style="text-align: left;" />
<?php endforeach; unset($col) ?>
</colgroup>
<thead>
<tr>
<?php foreach($cols as $col): ?>
<td><?php echo $col?></td>
<?php endforeach; unset($col)?>
</tr>
</thead>
<tbody>
<?php for($r = 1; $r <= $rows; $r++): ?>
<tr>
<?php foreach($cols as $key => $col): ?>
<td><?php echo ($key > 0) ? rand(1, 9999) : $r ?></td>
<?php endforeach; unset($col) ?>
</tr>
<?php endfor; ?>
</tbody>
</table>
</body>
</html>

View file

@ -0,0 +1,13 @@
my @heading = qw(X Y Z);
my $rows = 5;
print '<table><thead><td>',
(map { "<th>$_</th>" } @heading),
"</thead><tbody>";
for (1 .. $rows) {
print "<tr><th>$_</th>",
(map { "<td>".int(rand(10000))."</td>" } @heading),
"</tr>";
}
print "</tbody></table>";

View file

@ -0,0 +1,5 @@
(load "@lib/xhtml.l")
(<table> NIL NIL '(NIL (NIL "X") (NIL "Y") (NIL "Z"))
(for N 3
(<row> NIL N 124 456 789) ) )

View file

@ -0,0 +1,17 @@
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)

View file

@ -0,0 +1,8 @@
<table>
<tr><th></th><th>X</th><th>Y</th><th>Z</th></tr>
<tr><td style="font-weight: bold;">1</td><td>6040</td><td>4697</td><td>7055</td></tr>
<tr><td style="font-weight: bold;">2</td><td>2525</td><td>5468</td><td>6901</td></tr>
<tr><td style="font-weight: bold;">3</td><td>8851</td><td>3727</td><td>8379</td></tr>
<tr><td style="font-weight: bold;">4</td><td>5313</td><td>4396</td><td>1765</td></tr>
<tr><td style="font-weight: bold;">5</td><td>4013</td><td>5924</td><td>6082</td></tr>
</table>

View file

@ -0,0 +1,32 @@
/*REXX program to create an HTML table of five rows and three columns. */
arg rows .; if rows=='' then rows=5 /*no ROWS specified? Use default*/
cols=3
maxRand=9999 /*4-digit numbers, allow negative*/
headerInfo='X Y Z' /*column header information. */
oFID='a_table.html'
call lineout oFid,"<html>"
call lineout oFid,"<head></head>"
call lineout oFid,"<body>"
call lineout oFid,"<table border=5 cellpadding=20 cellspace=0>"
do r=0 to rows
if r==0 then call lineout oFid,"<tr><th></th>"
else call lineout oFid,"<tr><th>" r "</th>"
do c=1 for cols
if r==0 then call lineout oFid,"<th>" word(headerInfo,c) "</th>"
else call lineout oFid,"<td align=right>" rnd() "</td>"
end /*c*/
end /*r*/
call lineout oFid,"</table>"
call lineout oFid,"</body>"
call lineout oFid,"</html>"
exit
/*─────────────────────────────────────RND subroutine───────────────────*/
/*subroutine was subroutinized for better viewfulabilityness.*/
rnd: return right(random(0,maxRand*2)-maxRand,5) /*REXX doesn't gen negs*/

View file

@ -0,0 +1,17 @@
#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))

View file

@ -0,0 +1,22 @@
def r
rand(10000)
end
STDOUT << "".tap do |html|
html << "<table>"
[
['X', 'Y', 'Z'],
[r ,r ,r],
[r ,r ,r],
[r ,r ,r],
[r ,r ,r]
].each_with_index do |row, index|
html << "<tr>"
html << "<td>#{index > 0 ? index : nil }</td>"
html << row.map { |e| "<td>#{e}</td>"}.join
html << "</tr>"
end
html << "</table>"
end

View file

@ -0,0 +1,20 @@
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)

View file

@ -0,0 +1,26 @@
<table>
<tr>
<td></td>
<td>X</td>
<td>Y</td>
<td>Z</td>
</tr>
<tr>
<td>1</td>
<td>1358</td>
<td>6488</td>
<td>6434</td>
</tr>
<tr>
<td>2</td>
<td>2477</td>
<td>6493</td>
<td>1330</td>
</tr>
<tr>
<td>3</td>
<td>240</td>
<td>3038</td>
<td>9849</td>
</tr>
</table>

View file

@ -0,0 +1,21 @@
(define table #(
#("" "X" "Y" "Z")
#(1 1 2 3)
#(2 4 5 6)
#(3 7 8 9)))
(display "<table>")
(do ((r 0 (+ r 1))) ((eq? r (vector-length table)))
(display "<tr>")
(do ((c 0 (+ c 1))) ((eq? c (vector-length (vector-ref table r))))
(if (eq? r 0)
(display "<th>"))
(if (> r 0)
(display "<td>"))
(display (vector-ref (vector-ref table r) c))
(if (eq? r 0)
(display "</th>"))
(if (> r 0)
(display "</td>")))
(display "</tr>"))
(display "</table>")

View file

@ -0,0 +1,41 @@
# 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
}
# 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}]]
}
# 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}]
}]
}]
}]
}]