home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / lib / tclX-6.4 / help / intro / variables < prev   
Encoding:
Text File  |  1992-12-17  |  2.6 KB  |  50 lines

  1.      VARIABLES - SCALARS AND ARRAYS
  2.           Tcl allows the definition of variables and the use of  their
  3.           values either through $-style variable substitution, the set
  4.           command, or a few other mechanisms.  Variables need  not  be
  5.           declared:  a new variable will automatically be created each
  6.           time a new variable name is used.
  7.  
  8.           Tcl supports two types of variables:  scalars and arrays.  A
  9.           scalar  variable  has  a  single  value,  whereas  an  array
  10.           variable can have any number of elements, each with  a  name
  11.           (called  its  ``index'')  and a value.  Array indexes may be
  12.           arbitrary strings; they need not  be  numeric.   Parentheses
  13.           are  used  refer  to  array  elements  in Tcl commands.  For
  14.           example, the command
  15.  
  16.                set x(first) 44
  17.  
  18.           will modify the element of x whose index is  first  so  that
  19.           its   new  value  is  44.   Two-dimensional  arrays  can  be
  20.           simulated in Tcl by  using  indexes  that  contain  multiple
  21.           concatenated values.  For example, the commands
  22.  
  23.                set a(2,3) 1
  24.                set a(3,6) 2
  25.           set the elements of a whose indexes are 2,3 and 3,6.
  26.  
  27.           In general, array elements may be used anywhere in Tcl  that
  28.           scalar variables may be used.  If an array is defined with a
  29.           particular name, then there may not  be  a  scalar  variable
  30.           with  the  same  name.   Similarly,  if  there  is  a scalar
  31.           variable with a particular name then it is not  possible  to
  32.           make  array references to the variable.  To convert a scalar
  33.           variable to an array or  vice  versa,  remove  the  existing
  34.           variable with the unset command.
  35.  
  36.           The array command provides several features for dealing with
  37.           arrays,  such  as  querying the names of all the elements of
  38.           the array and searching through the array one element  at  a
  39.           time.
  40.  
  41.           Variables may be either global or local.  If a variable name
  42.           is  used  when  a  procedure  isn't  being executed, then it
  43.           automatically refers to a global variable.   Variable  names
  44.           used  within  a  procedure normally refer to local variables
  45.           associated with that invocation  of  the  procedure.   Local
  46.           variables  are  deleted  whenever  a  procedure  exits.  The
  47.           global command may be used to request that a name refer to a
  48.           global  variable  for  the duration of the current procedure
  49.           (this is somewhat analogous to extern in C).
  50.