TCL-TK SHELL SCRIPTING - PART 1(Q & A)

 1.    Write Tcl script to swap a set of character in two string [5]

Given: Two strings.

Task: To swap the set of characters in to strings.

Command used: string map

 

# s and t are variable having string in them.

 

 

2. Discuss variable substitution and command substitution in your words [5]

 Command substitution.

If a word contains an open bracket (``['') then Tcl performs command substitution. To do this it invokes the Tcl interpreter recursively to process the characters following the open bracket as a Tcl script. The script may contain any number of commands and must be terminated by a close bracket (``]''). The result of the script (i.e. the result of its last command) is substituted into the word in place of the brackets and all of the characters between them. There may be any number of command substitutions in a single word. Command substitution is not performed on words enclosed in braces.

  

Variable substitution.

If a word contains a dollar-sign (``$'') followed by one of the forms described below, then Tcl performs variable substitution: the dollar-sign and the following characters are replaced in the word by the value of a variable. Variable substitution may take any of the following forms:

$name

Name is the name of a scalar variable; the name is a sequence of one or more characters that are a letter, digit, underscore, or namespace separators (two or more colons).

 $name(index)

Name gives the name of an array variable and index gives the name of an element within that array. Name must contain only letters, digits, underscores, and namespace separators, and may be an empty string. Command substitutions, variable substitutions, and backslash substitutions are performed on the characters of index.

 ${name}

Name is the name of a scalar variable. It may contain any characters whatsoever except for close braces.

There may be any number of variable substitutions in a single word. Variable substitution is not performed on words enclosed in braces.

 

3.Write Tcl script to find average of 10 numbers with procedure [5]

# proc: Define a Tcl procedure.

# puts: Output a string to an I/O stream.

# foreach:  Loop construct over a list of values.

# expr: Evaluate a math expression.

# length: Return the number of elements in a list.

 

4. Write Tcl script to sum only even number between 0-10 using foreach command [5]

# if: Conditional command. Allows else and elseif clauses.

# return: Return a value from a procedure.

 

5. Write Tcl script for conversion of following with usage of format or/and procedure [10]

(a) a decimal number to other number system

(b) an octal number to other number system

# format: Format a string similar to C sprintf.

# %b- binary, %d – decimal, %x – hexa decimal , %o – octal conversion.

 


Post a Comment