next up previous contents index
Next: How can I change Up: FAQ Previous: How can I create

How can I create a structured type ?

TSF does not provide structured types. They can be simulated with hash tables or vectors. The following example creates and prints a list {300,200,100}. We recommand to use vectors when it is possible because hash tables are known to be slower and to use more memory.

SCRIPT MakeList (lst)
  // Initialize a list
  lst := NEWHASH 
  lst{"head"} := FALSE
ENDSCRIPT

SCRIPT AddToList(lst,val)
  head := lst{"head"}
  elem := NEWHASH 
  elem{"value"} := val
  elem{"next"} := head
  lst{"head"}  := elem 
ENDSCRIPT

SCRIPT PrintList(lst)
  current :=  lst{"head"} 
  WHILE (current)
    PRINT current{"value"}
    current:=current{"next"}
  ENDWHILE
ENDSCRIPT

SCRIPT main()
  CALLSCRIPT MakeList(&lst)
  CALLSCRIPT AddToList(lst,100)
  CALLSCRIPT AddToList(lst,200)
  CALLSCRIPT AddToList(lst,300)
  CALLSCRIPT PrintList(lst)
ENDSCRIPT



Yann Mevel
1999-04-30