Book Image

Tcl/Tk 8.5 Programming Cookbook

Book Image

Tcl/Tk 8.5 Programming Cookbook

Overview of this book

With Tcl/Tk, you can create full-featured cross-platform applications in a simple and easy-to-understand way without any expensive development package; the only tools required are a simple text editor and your imagination. This practical cookbook will help you to efficiently interact with editors, debuggers, and shell type interactive programs using Tcl/Tk 8. This cookbook will comprehensively guide you through practical implementation of Tcl/Tk 8.5 commands and tools. This book will take you through all the steps needed to become a productive programmer in Tcl/Tk 8. Right from guiding you through the basics to creating a stand-alone application, it provides complete explanation of all the steps along with handy tips and tricks. The book begins with an introduction to the Tcl shell, syntax, variables, and programming best practices in the language. It then explores procedures and the flow of events with control constructs followed by advanced error trapping and recovery. From Chapter 4, a detailed study of string expressions and handling enables you to handle various string functions and use lists to expand the string functionality. The book then discusses in-depth the Tcl Dictionary and how to utilize it to store and retrieve data. File operations and Tk GUI handling are covered extensively along with a developing a real-world address book application to practice the concepts learned.
Table of Contents (20 chapters)
Tcl/Tk 8.5 Programming Cookbook
Credits
About the Author
Acknowledgment
About the Reviewers
www.PacktPub.com
Preface

Computing mathematical expressions


In the following examples, we will see the correct syntax for both simple and complex mathematical formulas. To accomplish these computations, we will be using the Tcl expr command. The expr command, as its name implies, is used to evaluate mathematical expressions. This command can address everything from simple addition and subtraction to advanced computations such as sine and cosine. This removes the need to make system calls to perform advanced mathematical functions. The expr command evaluates the input and arguments and returns an integer, floating-point, or string value as appropriate.

A Tcl expression consists of a combination of operators, operands, and parenthetical containers (parenthesis, braces, or brackets). There are no strict typing requirements so any white space is stripped by the command automatically. Tcl supports non-numeric and string comparisons as well as Tcl specific operators.

As you will see, some computations may be performed without parenthetical notations; however, it is best to get into the habit of always using them. For example, expr 1+1 and expr (1+1) will both return a value of 2. While the omission of the parenthetical notation is completely acceptable in this usage of the expr command, I recommend developing the habit of always using them.

My personal favorite is the if-then-else expression. It provides a rapid method for comparison in a "single line" format. For example, if x and y are equal to 10, while z = 4 would be entered as expr ($x?$y:$z). This expression evaluates $x as a Boolean expression. If it's true the expression will return $y; if it's false, it returns $z.

Parenthetical notation is required for any operation that will access a specific mathematical function. For example: expr {pow (8, 4)} will access the mathematical power function and return a value of 4096.

Variable substitution is handled using the Tcl $ notation. The following example uses an x variable with a value of 4 and is entered as expr {pow (8, $x)}. This expression returns a value of 4096 as observed in the previous example. In the second case, $x has been processed with its variable value of 4.