This is not necessarily the current version of this TIP.
| TIP: | 77 |
| Title: | Support for Nested Paired Item Lists |
| Version: | $Revision: 1.3 $ |
| Author: | Christian Williams <xian at planetoutpartners dot com> |
| State: | Withdrawn |
| Type: | Project |
| Tcl-Version: | 8.5 |
| Vote: | Pending |
| Created: | Friday, 07 December 2001 |
| Obsoleted-By: | TIP #111 |
Tcl arrays can be transformed to and from lists using the array get and array set commands. This TIP proposes a new command for working directly these paired lists, and extending them to allow nesting in a manner analogous to TIP #22.
Tcl lists provide only ordinal access to their items; often it makes more sense to access items by pre-assigned descriptive names. This can be easily accomplished with Tcl arrays. Consider these alternatives:
set urlList { http tcl.activestate.com 80 /index.html }
array set urlArray {
proto http
host tcl.activestate.com
port 80
uri /index.html
}
Clearly the array approach promotes more readable code ($urlArray(host) versus [lindex $urlList 1]).
However, it's quite unwieldy and sometimes expensive to use arrays to access members of many sets of structured data, particularly when that data contains nested structures.
Consider this structured data:
set data {
text {ignored-data}
valid-styles {
justification {left centered right full}
font {courier helvetica times}
}
}
Extracting items from structures like this can be accomplished by multiple array set commands:
array set dataArray $data array set validStylesArray $dataArray(valid-styles) puts "Justification: $validStylesArray(justification)"
To modify an item in struct, we need some pretty ugly code:
array set dataArray $struct
array set validStylesArray $dataArray(valid-styles)
set validStylesArray(justification) {left}
set dataArray(valid-styles) [array get validStylesArray]
set data [array get dataArray]
Clearly, all this setting and getting of arrays imposes a rather high overhead; many variables are created and moved around. Also, if this is occurring in a loop, then care must be taken to unset the dataArray and vali