:TITLE: Associative Arrays. ;# ;# RCSID: $Header: /cvsroot/tcl/tcltutorial/original/Tcl22.lsn,v 1.1 2004/11/04 16:01:14 davidw Exp $ ;# Copyright (c) 1995 Clif Flynt ;# 9300 Fleming Rd. ;# Dexter, MI 48130 ;# clif@cflynt.com ;# See file "NOTICE" for licensing terms. ;# :LESSON_TEXT_START_LEVEL 0: Like awk and perl, Tcl supports associative arrays.
The syntax for an associative array is to put the index within parentheses.
There are several array commands aside from simply accessing and creating arrays which will be discussed in this and the next lesson.
When an associative array name is given as the argument to the
global command, all the elements of the associative array
become available to that proc. For this reason, Brent Welch
recommends ( in
The syntax for an associative array is to put the index within parentheses.
There are several array commands aside from simply accessing and creating arrays which will be discussed in this and the next lesson.
When an associative array name is given as the argument to the
global command, all the elements of the associative array
become available to that proc. For this reason, Brent Welch
recommends ( in
This method makes it simpler to share data between many procs that are
working together, and doesn't pollute the global namespace as badly as
using separate globals for all shared data items.
:TEXT_END:
:LESSON_TEXT_START_LEVEL 2:
There are three simple data types in Tcl; the list, the string, and the
associative array. An associative array is a collection of data elements
similar to a list. In a list, every element has a location, and you can
access a data element by that location (with the lindexcommand).
With associative arrays, every data element has a name as well as a value,
and you can access a particular element by its name.
An item in an associative array is described by a string in this format:
arrayName(elementName). As with regular variables, you can
set the value of an element in an array with a commands like:
set chores(Sunday) "Wash Car";
set chores(Monday) "Take out Garbage"
set chores(Tuesday) [list {Clean Fridge} {Go shopping} {Call Mom}];
There are several array commands aside from simply accessing and creating
arrays which will be discussed in this and the next lesson.
When an associative array name is given as the argument to the
global command, all the elements of the associative array
become available to that proc. For this reason, Brent Welch
recommends ( in
This method makes it simpler to share data between many procs that are working together, and doesn't pollute the global namespace as badly as using separate globals for all shared data items.
For example, consider a program which has several procs for handling different calculations each of which has a subtotal that has to be carried forward from proc to proc, and all feed into the same total. You could handle it like this:
proc proc1 {} {
global subtotal1, total;
...
}
proc proc2 {} {
global subtotal2, total;
...
}
...
or like this
proc proc1 {} {
global data;
set data(subtotal1) ...
}
proc proc2 {} {
global data;
set data(subtotal2) ...
}
...
In the second method, there is only one global variable name created. This
give you a much lower chance of colliding with other global variables when
you merge your set of procs with other peoples code to make new programs.
Using the associative array also makes it easier to add a new field if it becomes necessary. You can just add the new data element to the associative array, and it becomes available to any procs that need to access it.
A fairly common bug in Tcl programs is neglecting to put in the global
command for a global variable. If this happens, Tcl will silently make a local
variable with the same name as the global variable, and will use it in that
proc, but any changes you make to that variable won't be reflected in the global
scope, and the current state of the global variable won't be available in the
proc. This creates code that is syntactically correct, but doesn't produce the
expected results.
:TEXT_END:
:CODE_START:
array set array1 [list {123} {Abigail Aardvark} \
{234} {Bob Baboon} \
{345} {Cathy Coyote} \
{456} {Daniel Dog} ]
puts "Array1 has [array size array1] entries\n"
puts "Array1 has the following entries: \n [array names array1] \n"
puts "ID Number 123 belongs to $array1(123)\n"
if {[array exist array1]} {
puts "array1 is an array"
} else {
puts "array1 is not an array"
}
if {[array exist array2]} {
puts "array2 is an array"
} else {
puts "array2 is not an array"
}
:TEXT_END: