Tcl Library Source Code

EuroTcl/OpenACS 11 - 12 JULY 2024, VIENNA

[ Main Table Of Contents | Table Of Contents | Keyword Index | Categories | Modules | Applications ]


struct::record - Define and create records (similar to 'C' structures)

Table Of Contents


package require Tcl 8.5 9
package require struct::record ?1.2.3?

record define recordName recordMembers ?instanceName1 instanceName2 ...?
record show record
record show instances recordName
record show members recordName
record show values instanceName
record exists record recordName
record exists instance instanceName
record delete record recordName
record delete instance instanceName
instanceName cget -member
instanceName cget -member1 -member2
instanceName cget
instanceName configure
instanceName configure -member value
instanceName configure -member1 value1 -member2 value2
recordName instanceName|#auto ?-member1 value1 -member2 value2 ...?
instanceName cget ?-member1 -member2 ...?
instanceName configure ?-member1 value1 -member2 value2 ...?


The ::struct::record package provides a mechanism to group variables together as one data structure, similar to a C structure. The members of a record can be variables or other records. However, a record can not contain circular records, i.e. records that contain the same record as a member.

This package was structured so that it is very similar to how Tk objects work. Each record definition creates a record object that encompasses that definition. Subsequently, that record object can create instances of that record. These instances can then be manipulated with the cget and configure methods.

The package only contains one top level command, but several sub commands (see below). It also obeys the namespace in which the record was defined, hence the objects returned are fully qualified.


Record members can either be variables, or other records, However, the same record can not be nested witin itself (circular). To define a nested record, you need to specify the record keyword, along the with name of the record, and the name of the instance of that nested record (within the container). For example, it would look like this:

# this is the nested record
record define mynestedrecord {

# This is the main record
record define myrecord {
    {record mynestedrecord mem3}

You can also assign default or initial values to the members of a record, by enclosing the member entry in braces:

record define myrecord {
    {mem2 5}

All instances created from this record definition will initially have 5 as the value for member mem2. If no default is given, then the value will be the empty string.

Getting Values

To get a value of a member, there are several ways to do this.

Setting Values

To set a value of a member, there are several ways to do this.

Alias access

In the original implementation, access was done by using dot notation similar to how C structures are accessed. However, there was a concensus to make the interface more Tcl like, which made sense. However, the original alias access still exists. It might prove to be helpful to some.

Basically, for every member of every instance, an alias is created. This alias is used to get and set values for that member. An example will illustrate the point, using the above defined records:

% # Create an instance first
% myrecord inst1

% # To get a member of an instance, just use the alias. It behaves
% # like a Tcl command:
% inst1.mem1

% # To set a member via the alias, just include a value. And optionally
% # the equal sign - syntactic sugar.
% inst1.mem1 = 5

% inst1.mem1

% # For nested records, just continue with the dot notation.
% # note, no equal sign.
% inst1.mem3.nest1 10

% inst1.mem3.nest1

% # just the instance by itself gives all member/values pairs for that
% # instance
% inst1
-mem1 5 -mem2 {} -mem3 {-nest1 10 -nest2 {}}

% # and to get all members within the nested record
% inst1.mem3
-nest1 10 -nest2 {}


The following subcommands and corresponding arguments are available to any record command:


The following subcommands and corresponding arguments are available to any record instance command:


Two examples are provided to give a good illustration on how to use this package.

Example 1 - Contact Information

Probably the most obvious example would be to hold contact information, such as addresses, phone numbers, comments, etc. Since a person can have multiple phone numbers, multiple email addresses, etc, we will use nested records to define these. So, the first thing we do is define the nested records:

##  This is an interactive example, to see what is returned by
##  each command as well.

% namespace import ::struct::record::*

% # define a nested record. Notice that country has default 'USA'.
% record define locations {
    {country USA}
% # Define the main record. Notice that it uses the location record twice.
% record define contacts {
    {record locations home}
    {record locations work}
% # Create an instance for the contacts record.
% contacts cont1
% # Display some introspection values
% record show records
::contacts ::locations
% #
% record show values cont1
-first {} -middle {} -last {} -home {-street {} -street2 {} -city {} -state {} -zipcode {} -country USA -phone {}} -work {-street {} -street2 {} -city {} -state {} -zipcode {} -country USA -phone {}}
% #
% record show instances contacts
% #
% cont1 config
-first {} -middle {} -last {} -home {-street {} -street2 {} -city {} -state {} -zipcode {} -country USA -phone {}} -work {-street {} -street2 {} -city {} -state {} -zipcode {} -country USA -phone {}}
% #
% cont1 cget
-first {} -middle {} -last {} -home {-street {} -street2 {} -city {} -state {} -zipcode {} -country USA -phone {}} -work {-street {} -street2 {} -city {} -state {} -zipcode {} -country USA -phone {}}
% # copy one record to another record
% record define contacts2 [record show members contacts]
% record show members contacts2
first middle last {record locations home} {record locations work}
% record show members contacts
first middle last {record locations home} {record locations work}

Example 2 - Linked List

This next example just illustrates a simple linked list

% # define a very simple record for linked list
% record define linkedlist {
% linkedlist lstart
% lstart config -value 1 -next [linkedlist #auto]
% [lstart cget -next] config -value 2 -next [linkedlist #auto]
% [[lstart cget -next] cget -next] config -value 3 -next "end"
% set next lstart
% while 1 {
    lappend values [$next cget -value]
    set next [$next cget -next]
    if {[string match "end" $next]} break
% puts "$values"
1 2 3
% # cleanup linked list
% # We could just use delete record linkedlist also
% foreach I [record show instances linkedlist] {
    record delete instance $I
% record show instances linkedlist

Bugs, Ideas, Feedback

This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category struct :: record of the Tcllib Trackers. Please also report any ideas for enhancements you may have for either package and/or documentation.

When proposing code changes, please provide unified diffs, i.e the output of diff -u.

Note further that attachments are strongly preferred over inlined patches. Attachments can be made by going to the Edit form of the ticket immediately after its creation, and then using the left-most button in the secondary navigation bar.


data structures, record, struct


Data structures


Copyright © 2002, Brett Schwarz