struct::record(3tcl)
πΈ NAME πΈ
struct::record β the struct::record(3tcl) situation, from the Tcl Data Structures manual. We're defining and creating records (basically C structs but make it Tcl) π
β¨ SYNOPSIS β¨
package require Tcl 8.5 9
package require struct::record ?1.2.4?
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
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 ...?
π¦ DESCRIPTION π¦
okay so the ::struct::record package is literally here to let you group a bunch of variables together into one data structure, exactly like a C struct. iconic behavior fr fr π
The members of a record can be variables or other records. BUT β and this is the one thing you cannot do β a record can not contain circular records, meaning records that contain the same record as a member. no cap, it's a no. π«
This package was structured so it feels super similar to how Tk objects work. Each record definition creates a record object that encompasses that definition. Then, that record object can create instances of that record. And those instances get manipulated with the cget and configure methods. it's giving object-oriented but Tcl β¨
The package only has one top level command, but several sub commands (scroll down bestie). It also obeys the namespace in which the record was defined, so the objects you get back are fully qualified. π«Ά
record define recordName recordMembers ?instanceName1 instanceName2 ...?β Defines a record.recordNameis the name of the record, and it doubles as an object command. That object command is what you use to create instances of the record definition. TherecordMembersare the members that make up the record definition β variables and other records. And if you pass the optionalinstanceNameargs, then an instance gets generated for eachinstanceNameright after the definition is created. two birds, one stone πrecord show recordβ Returns a list of records that have been defined.record show instances recordNameβ Returns the instances that have been instantiated byrecordName.record show members recordNameβ Returns the members that are defined for recordrecordName. It returns the same format as how the records were defined.record show values instanceNameβ Returns a list of values that are set for the instanceinstanceName. The output is a list of key/value pairs. If there are nested records, then the values of the nested records will itself be a list. π€―record exists record recordNameβ Tests for the existence of a record with the namerecordName.record exists instance instanceNameβ Tests for the existence of a instance with the nameinstanceName.record delete record recordNameβ DeletesrecordName, and all instances ofrecordName. It will return an error if the record does not exist.record delete instance instanceNameβ Deletes instance with the name ofinstanceName. It will return an error if the instance does not exist. Note that this recursively deletes any nested instances as well. π
π§ RECORD MEMBERS π§
Record members can either be variables, or other records. However, the same record can not be nested within itself (circular). To define a nested record, you need to specify the record keyword, along with the 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 {
nest1
nest2
}
# This is the main record
record define myrecord {
mem1
mem2
{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 {
mem1
{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. (yes, the void, the nothingness, the {} π)
π GETTING VALUES π
To get a value of a member, there are several ways to do this.
instanceName cget -memberβ In this form the built-incgetinstance method returns the value of the specified member. Note the leading dash. β οΈ
To reach a nested member use dot notation:
instanceName cget -mem3.nest1
instanceName cget -member1 -member2β In this form the built-incgetinstance method returns a list containing the values of both specified members, in the order of specification.instanceName cgetinstanceName configureinstanceName
These forms are all equivalent. They return a dictionary of all members and the associated values. same vibe, different spelling π
π SETTING VALUES π
To set a value of a member, there are several ways to do this.
instanceName configure -member valueβ In this form the built-inconfigureinstance method sets the specified member to the given value. Note the leading dash. β οΈ
To reach a nested member use dot notation:
instanceName configure -mem3.nest1 value
instanceName configure -member1 value1 -member2 value2β In this form the built-inconfigureinstance method sets all specified members to the associated values.
π 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. (the girlies who loved the old way, we see you π«Ά)
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
::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
5
% inst1.mem1
5
% # For nested records, just continue with the dot notation.
% # note, no equal sign.
% inst1.mem3.nest1 10
10
% inst1.mem3.nest1
10
% # 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 {}
π₯ RECORD COMMAND π₯
The following subcommands and corresponding arguments are available to any record command:
recordName instanceName|#auto ?-member1 value1 -member2 value2 ...?β Using therecordNameobject command that was created from the record definition, instances of the record definition can be created. Once an instance is created, it inherits the members of the record definition, very similar to how objects work. During instance generation, an object command for the instance is created as well, usinginstanceName. π
This object command is used to access the data members of the instance. During the instantiation, while values for that instance may be given, when done, all values must be given, and be given as key/value pairs, like for method configure. Nested records have to be in list format. (no half-sending bestie, it's all or nothing π€)
Optionally, #auto can be used in place of instanceName. When #auto is used, the instance name will be automatically generated, and of the form recordNameN, where N is a unique integer (starting at 0) that is generated. main character energy, auto-named β¨
π INSTANCE COMMAND π
The following subcommands and corresponding arguments are available to any record instance command:
instanceName cget ?-member1 -member2 ...?β Each instance has the methodcget. This is very similar to how Tk widget'scgetcommand works. It queries the values of the members for that particular instance. If no arguments are given, then a dictionary is returned.instanceName configure ?-member1 value1 -member2 value2 ...?β Each instance has the methodconfigure. This is very similar to how Tk widget'sconfigurecommand works. It sets the values of the particular members for that particular instance. If no arguments are given, then a dictionary list is returned. π
β¨ EXAMPLES β¨
Two whole examples, bestie β they're here to show you exactly how to use this package. No guessing, no vibes-based programming. π
πΈ EXAMPLE 1 - CONTACT INFORMATION πΈ
Okay so the most obvious use case ever: holding contact info. Addresses, phone numbers, comments, all of it. And because one person can lowkey have multiple phone numbers and multiple email addresses (main character with a work phone and a personal phone, we love to see it), we're gonna use nested records to define those. So first thing first, we 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 {
street
street2
city
state
zipcode
{country USA}
phone
}
::locations
% # Define the main record. Notice that it uses the location record twice.
% record define contacts {
first
middle
last
{record locations home}
{record locations work}
}
::contacts
% # Create an instance for the contacts record.
% contacts cont1
::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
% #
% 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]
::contacts2
% 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}
%
See how country just β¨*defaulted*β¨ to USA without us lifting a finger? Iconic. And contacts2 got born straight out of record show members contacts β copy-paste but make it structural. π
π¦ EXAMPLE 2 - LINKED LIST π¦
This next one is just a cute lil' simple linked list, nothing deep:
% # define a very simple record for linked list
% record define linkedlist {
value
next
}
::linkedlist
% linkedlist lstart
::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
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
%
Walking the chain with that while 1 loop and breaking on "end"? Chef's kiss. And then tidying up every instance with a foreach over record show instances linkedlist β cleanliness is next to godliness fr fr. π§Ήβ¨
π BUGS, IDEAS, FEEDBACK π
Not to be dramatic but this document β and the package it describes β will undoubtedly contain bugs and other problems. It's giving "we're all human." So if you find something, please report it in the category struct :: record of the Tcllib Trackers at [http://core.tcl.tk/tcllib/reportlist].
And listen β if you've got ideas for enhancements, for either the package and/or the documentation, send those too. We want them. π«Ά
When you're proposing code changes, please provide unified diffs, i.e. the output of diff -u.
Also important bestie: attachments are strongly preferred over inlined patches. You can attach stuff by going to the Edit form of the ticket right after creating it, and then using the left-most button in the secondary navigation bar. Don't skip this step, it's not a vibe, it's a requirement. π
π KEYWORDS π
data structures, record, struct
π CATEGORY π
Data structures
π COPYRIGHT π
Copyright (c) 2002, Brett Schwarz <brett_schwarz@yahoo.com>
okay that's the whole page babes, we made it to the end π this was tcllib version 1.2.4, and the page is struct::record(3tcl) β go forth and define your records, you absolute legend β¨π
π¦
π¦ where this page comes from
- debian trixie: tcllib 2.0+dfsg-4 Β· Tcl
- ubuntu questing: tcllib 2.0+dfsg-4
- ubuntu resolute: tcllib 2.0+dfsg-5 Β· Tcl
- ubuntu stonking: tcllib 2.0+dfsg-6 Β· Tcl
- Copyright (c) 2002, Brett Schwarz <brett_schwarz@yahoo\&.com>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms