Previous Topic

Next Topic

...Read External files

External files may either contain Tcl commands or data.

The external file may contain a series of commands that is common in most analyses. One set of Tcl commands that can be stored in a external file are ones which define units.

An example of an external file that may want to be read within the input commands is the unit-definition file presented earlier (units&constants.tcl).

This file is invoked with the following command:

source units.tcl

An external file may contain a series of calculations that are repeated. An example of this is a parameter study:

set Hcolumn 66;

source analysis.tcl

set Hcolumn 78;

source analysis.tcl

The analysis.tcl file contains the commands that set up and execute the entire analysis.

The following commands open a data file (filename=inFilename), read the file row by row and assign the value of each row to the a single variable (Xvalues). If there are more than one value in the row, $Xvalues is a list array, and the individual components may be extracted using the lindex command. The user may change the commands to be exectued once the data-line has been read to whatever is needed in the analysis.

# ----ReadData.tcl--------------------------------------------------------------------

if [catch {open $inFilename r} inFileID] {; # Open the input file and check for error

puts stderr "Cannot open $inFilename for reading"; # output error statement

} else {

foreach line [split [read $inFileID] \n] { ; # Look at each line in the file

if {[llength $line] == 0} {; # Blank line --> do nothing

continue;

} else {

set Xvalues $line; # execute operation on read data

}

}

close $inFileID; ; # Close the input file

}