Emulating Tables

When a programming language does not support "nimble" tables or the implementation is too slow, there are various ways to emulate them. Here are some suggestions.

A map nested in a map is probably conceptually the simplest, but in practice can get a bit tricky in some languages. A variation is a map of "records", where "record" may be a C-like "struc" or an object.


List plus Maps

A list can be used to store the primary key for iteration purposes and associative arrays (maps) can be used to store individual cell values. Example three-column table:

 keyList = "aaa,bbb,ccc,etc";
 columnA = newMap();
 columnB = newMap();
 columnC = newMap();
 // [fill up from table source, not shown]
 for (i = listEach(keyList,",")) {
   printLn(columnA[i]);
   printLn(columnB[i]);
   printLn(columnC[i]);
 }


Row-Column Hash

Row ID and column name can be concatenated together using a reserved character such as a comma or a pipe.

 table = newMap();
 // [fill up from table source, not shown]
 for (i = listEach(keyList,",")) {   // see above example for key-list source
   printLn(table[i . "|" . "columnA"]);
   printLn(table[i . "|" . "columnB"]);
   printLn(table[i . "|" . "columnC"]);
 }

This approach may be the most flexible in that one does not have to hard-code columns up-front. Thus, it can serve as part of a generic MinimalTable framework in a scriptish language. Accessor routines/methods can be created to simplify references and give alternative delimiter choices.


EditText of this page (last edited January 30, 2008) or FindPage with title or text search