Recent Changes - Search:


Mod /

Tables

(:toc:) (:toc:)

Tables using *table

The *table utility provides compact definition of data tables wherein columns names are given in the first row.

If the first element of the initialing list is a single non-alphanumeric character, it specifies the table type, otherwise the table is a simple table. The currently supported types are "*", "#" and "^".

Simple Tables

A simple table is simply a list of rows where the first element is not a single non-alphanumeric character, eg.

  *table workers {
     { First Last     Age }
     { Bob   Brown    19  }
     { Tom   Wake     18  }
     { Tommy Smart    14  }
     { Bill  Williams 17  }
  }

  $workers  update 1  Age 21;  # Update Bobs age.
  set id [$workers find -name Tom -key First]
  $workers  incr $id  Age 2

  pack [*treeview .t -tree $workers -flat 1] -fill both -expand y

Records get inserted starting from node id 1. This form of table is less useful because it lacks symbolic indexes.

Indexed Tables

If the first element in the list is "*", then it is an indexed *table. The second element contains the column titles. The rest of the list contains index/row pairs.

Programatically, rows can be indexed with a 0-> preceding the index label. Note that no real checking is performed, eg. duplicate index labels are accepted. Also new column-keys may be created by the user.

  *table staff {
     *     { First Last     Age }
     bob   { Bob   Brown    19  }
     tom   { Tom   Wake     18  }
     tom   { Tommy Smart    14  }
     bill  { Bill  Williams 17  }
  }

  $staff  set  0->bob  Last Brown-Levy
  $staff  incr 0->tom  Age  2;  # Matches 1st entry "Tom Wake"
  $staff  set  0->bill Sex  M;  # Creates the new key Sex.

  toplevel .s
  pack [*treeview .s.t -tree $staff] -fill both -expand y

Validated Indexed Tables

If the first list element is a "#" then it is a validated indexed *table.

Programatically, elements can be indexed directly with the label (because it was also added as a tag). An error will be raised if a duplicate index is inserted. Also invalid is inserting new key names.

  *table managers {
     #     { First  Last     Age }
     bob   { Bob    Brown    19  }
     tom   { Tom    Wake     18  }
     bill  { Bill   Williams 17  }
  }

  $managers  incr   bob    Age 2
  $managers  update bill   Last Barry

  catch { $managers insert end -label bob }
  # This is an error as bob is already defined.

  catch { $managers set bob   Sex M }
  # This is an error as there is no key Sex.

  toplevel .m
  pack [*treeview .m.t -tree $managers] -fill both -expand y

 }

Struct Tables

If the first list element is a "^" then it is a struct *table. This is like a validated indexed table, but uses a struct definition to obtain field names, eg:

   *struct new user {
      { First     {}  "First name of user" }
      { Last      {}  "Last name of user" }
      { Age       0   "Age of user" -type Int }
  }

  *table managers {
     ^     user
     bob   { Bob    Brown    19  }
     tom   { Tom    Wake     18  }
     bill  { Bill   Williams 17  }
  }

  $managers update bill   Last Barry

A struct provides default values for ommitted data fields. It also supports typechecking. See *struct for details.

Edit - History - Print - Recent Changes - Search
Page last modified on February 03, 2010, at 08:40 AM