Version 9 of keyed list

Updated 2008-11-13 20:00:14 by LarrySmith

Purpose: to dicuss TclX's special keyed list [L1 ] functions.

From the TclX man page, you can read:

A keyed list is a list in which each element contains a key and value pair. These element pairs are stored as lists themselves, where the key is the first element of the list, and the value is the second. The key-value pairs are referred to as fields. This is an example of a keyed list:

 {{NAME {Frank Zappa}} {JOB {musician and composer}}}

Fields may contain subfields; `.' is the separator character. Subfields are actually fields where the value is another keyed list. Thus the following list has the top level fields ID and NAME, and subfields NAME.FIRST and NAME.LAST:

 {ID 106} {NAME {{FIRST Frank} {LAST Zappa}}}

There is no limit to the recursive depth of subfields, allowing one to build complex data structures.

Keyed lists are constructed and accessed via a number of commands. All keyed list management commands take the name of the variable containing the keyed list as an argument (i.e. passed by reference), rather than passing the list directly.

  • keyldel listvar key
          Delete the field specified by key from the keyed list in the
          variable listvar.  This removes both the key and the value
          from the keyed list.
  • keylget listvar ?key? ?retvar | {}?
          Return the value associated with key from the keyed list in
          the variable listvar.  If retvar is not specified, then the
          value will be returned as the result of the command. In this
          case, if key is not found in the list, an error will result.

          If retvar is specified and key is in the list, then the
          value is returned in the variable retvar and the command
          returns 1 if the key was present within the list.  If key
          isn't in the list, the command will return 0, and retvar
          will be left unchanged.  If {} is specified for retvar, the
          value is not returned, allowing the Tcl programmer to 
          determine if a key is present in a keyed list without setting a
          variable as a side-effect.

          If key is omitted, then a list of all the keys in the keyed
          list is returned.
  • keylkeys listvar ?key?
          Return the a list of the keys in the keyed list in the 
          variable listvar.  If keys is specified, then it is the name of
          a key field  who's subfield keys are to be retrieve.
  • keylset listvar key value ?key2 value2 ...?
          Set the value associated with key, in the keyed list 
          contained in the variable listvar, to value.  
          If listvar does not exists, it is created.  
          If key is not currently in the list, it will be added.  
          If it already exists, value replaces the existing value.  
          Multiple keywords and values may be specified, if desired.

Is there a way we can ignore dot as separator & treat a whole string together?


DKF: In new code, consider using dict instead. The format's different, but the capabilities are similar.


keylget returns only the value of the 1st element with matching key in the list evenif there are multiple same elements.

e.g, in the list {{inventory {{member {{member-type "PC"}}} {member {{member-type "LAPTOP"}}}}}

Here inventory.member.member-type will return only "PC".

I have written a code to get all the elements. Where can I upload the code for comments?

LV If you have the code on a web page or in a web accessible source code repository, just mention the URL here. If you don't have something like that, Google and yahoo both make various resource available for people to make code, etc. available. However, the recommendation is often made these days to moving to Tcl 8.5 and making use of dict instead of Tclx's keyed lists, because dict is going to be supported better than keyed lists are.

Larry Smith One problem I have with both dicts and keyed lists is that each one drags around it's own copy of the field names. Not a problem when you have dozens of records, big problem when you have thousands. That's why I wrote records. With records there is just one master copy of the field names, and thereafter each record of that same type only needs the data. I think this is much superior (he said, humbly :).