next up previous contents index
Next: Various functions Up: Various attributes and functions Previous: Controlling the target

Using vectors and hash tables

TSF provides two types for the manipulation of set of values ; the vectors and the hash tables. A vector is an sequence of values referenced by an integer index. The lower index is 1.

  $\blacktriangleright$ V$\lbrack$i$\rbrack$
 
The expression references the element of index i in the vector V.
  $\blacktriangleright$ VECTOR(v1,v2,...,vn)
 
The expression returns a vector of length n initialized with the values v1,v2,...,vn of any types.
  $\blacktriangleright$ NEWVECTOR(n,v)
 
The expression returns a vector of length n. All elements are initialized with the same value v.

The following expressions can be used to manipulate hash tables.

  $\blacktriangleright$ H$\{$k$\}$
 
The expression returns the element of key k in the hash table H. The value FALSE is returned when the key is not found in the hash table.
  $\blacktriangleright$ NEWHASH()
 
The expression returns an empty hash table.
  $\blacktriangleright$ CLEARHASH(H)
 
The statement remove all elements in the hash table H.
  $\blacktriangleright$ HASHKEYS(H)
 
The expression returns a vector initialized with the keys in the hash table H.
  $\blacktriangleright$ HASHVALUES(H)
 
The expression returns a vector initialized with the values in the hash table H. The values are given in the same order of the keys returned by the function HASHKEYS.

The following are commons to vectors and hash tables.

  $\blacktriangleright$ LENGTH(M)
 
The expression returns the number of elements in the vector or the hash table M.

$\vartriangleright$ Example 52:Print the content of a hash table.

  SCRIPT DumpHashTable(H)
    // Version with the keys
    keys := HASHKEYS(H)
    FOR i := 1,LENGTH(keys) DO
      k := keys[i]
      PRINT TOSTRING(k)+" => "+TOSTRING(H{k})
    ENDFOR
  ENDSCRIPT
  SCRIPT ShortDumpHashTable(H)
    // Version without the keys
    val := HASHVALUES(H)
    FOR i := 1,LENGTH(val) DO
      PRINT val[i]
    ENDFOR
  ENDSCRIPT

$\vartriangleright$ Example 53:A simple use of vectors and hash tables. Associate a string (in english) to its numerical value.

  SCRIPT main()
    // Create a vector of number (strings)
    v1 := VECTOR("one","two","three","four")
    // Create the equivalent vector of number (integers)
    v2 := NEWVECTOR(LENGTH(v1),FALSE)
    FOR i:=1,LENGTH(v2) DO
     v2[i] := i 
    ENDFOR
    // Associate v1 and v2 in a hash table
    h := NEWHASH 
    FOR i:=1,LENGTH(v1) DO
      h{v1[i]} := v2[i] 
    ENDFOR
    // Print the value associated to the string "two" 
    PRINT "The value of 'two' is "
    PRINT h{"two"}    
  ENDSCRIPT

In the current version of TSF, there are some syntaxical restrictions in the usage of vectors and hash tables. A element can not be used as a reference in a function call or in a script call. For example, it is not possible to write the following statement:

CALLSCRIPT my_script(&V[3])
When accessing an element (with $\lbrack\rbrack$ or $\{\}$), the left part must be a variable name. So, V[3] is allowed but not V[3][4] (i.e. the left part V[3] is not a variable name).


next up previous contents index
Next: Various functions Up: Various attributes and functions Previous: Controlling the target
Yann Mevel
1999-04-30