next up previous contents
Next: Command echo and messaging Up: Miscellaneous commands Previous: Miscellaneous commands

Conditional execution

  

if Conditionally execute a command or a script
{-exist <nameent> | check, if name entry exists
-notexist <nameent>} | check, if name entry does not exist
-expr <expression>; normal conditional expression
-cmd <cmdline>: command to be executed, if condition is true
while Conditionally repeat (a command or) a script
-expr <expression> ; normal conditional expression
-cmd <cmdline>: command(s) to be repeated, as long as condition is true
[-loop <loopcmd>:] command to be executed after each execution of <cmdline>

These commands, if and while, allow conditional execution of commands or script files. if first checks, whether a name entry exists, or evaluates the specified expression. If the specified condition is true, if executes the chosen command or script.

while first evaluates specified <expression> and, if it is true, executes the specified <cmdline>. Then it also executes <loopcmd> (if specified) and then the expression is re-evaluated (and, if true, script re-run), until the expression becomes false. If the loop is based on the value of some name entry, it's value should be altered within the repeated script or in <loopcmd>.

Example: To draw a bar showing the average of each data field:

# Load boston data, train a SOM, calculate stats and create a window
NDA> load boston.dat
NDA> prepro -d boston -dout prep -n
NDA> somtr -d prep -sout som -cout cld -l 5
...
NDA> clstat -c cld -d boston -dout stats -avg
NDA> mkgrp win -s som
NDA> setgdat win -d stats
NDA> setgcld win -c cld
NDA> layer win -l 3
NDA> show win
# Calculate the number of data fields
NDA> len -n boston -fout lkm
# Run a loop from 0 to lkm - 1
NDA> expr -fout ind -expr 0;
NDA> while -expr 'ind' < 'lkm'; -cmd macbar:
NDA> draw /win
...

Contents of script macbar:

#
# ${boston[${ind}]} evaluates into the name of {ind}th field of frame
#
bar win -f stats.${boston[${ind}]}_avg -co ${ind}
expr -fout ind -expr 'ind' + 1;

Example: The same can also be achieved with:

# Load boston data, train a SOM, calculate stats and create a window
NDA> load boston.dat
NDA> prepro -d boston -dout prep -n
NDA> somtr -d prep -sout som -cout cld -l 5
...
NDA> clstat -c cld -d boston -dout stats -avg
NDA> mkgrp win -s som
NDA> setgdat win -d stats
NDA> setgcld win -c cld
NDA> layer win -l 3
# Calculate the number of data fields
NDA> len -n boston -fout lkm
# Run a loop from 0 to lkm - 1
NDA> expr -fout ind -expr 0;
NDA> while -expr 'ind' < 'lkm';
      -cmd bar win -f \stats.${boston[${ind}]}_avg -co ${ind}\:
      -loop expr -fout ind -expr 'ind' + 1;:
NDA> show win


next up previous contents
Next: Command echo and messaging Up: Miscellaneous commands Previous: Miscellaneous commands

Anssi Lensu
Wed Oct 6 12:57:48 EET DST 1999