Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Tcl/Tk 8.5 Programming Cookbook

You're reading from   Tcl/Tk 8.5 Programming Cookbook With over 100 recipes, this Cookbook is ideal for both beginners and advanced Tcl/Tk programmers. From the basics to creating applications, it‚Äôs full of indispensable tips and tricks to make the most of the language.

Arrow left icon
Product type Paperback
Published in Feb 2011
Publisher Packt
ISBN-13 9781849512985
Length 236 pages
Edition 1st Edition
Languages
Arrow right icon
Toc

Table of Contents (20) Chapters Close

Tcl/Tk 8.5 Programming Cookbook
Credits
About the Author
1. Acknowledgment
About the Reviewers
2. www.PacktPub.com
3. Preface
1. The Tcl Shell FREE CHAPTER 2. Using the Building Blocks Control Constructs 3. Error Handling 4. Handling String Expressions 5. Expanding String Functionality Using Lists 6. The Tcl Dictionary 7. File Operations 8. Tk GUI Programming with Tcl/Tk 9. Configuring and Controlling Tk Widgets 10. Geometry Management 11. Using Tcl Built-in Dialog Windows 12. Creating and Managing Menus 13. Creating the Address Book Application

Tcl expr operands


Tcl operands are treated as integers, where feasible. They may be specified as decimal, binary (first two characters must be 0b), hexadecimal (first two characters must be 0x), or octal (first two characters must be 0o). Care should be taken when passing integers with a leading 0, for example 08, as the interpreter would evaluate 08 as an illegal octal value. If no integer formats are included, the command will evaluate the operand as a floating-point numeric value. For scientific notations, the character e (or E) is inserted as appropriate. If no numeric interpretation is feasible, the value will be evaluated as a string. In this case, the value must be enclosed within double quotes or braces. Please note that not all operands are accepted by all operators. To avoid inadvertent variable substitution, it is always best to enclose the operands within braces. For example, take a look at the following:

  • expr 1+1*3 will return a value of 4.

  • expr (1+1)*3 will return a value of 6.

Operands may be presented in any of the following:

Operand

Explanation

Numeric

Integer and floating-point values may be passed directly to the command.

Boolean

All standard Boolean values (true, false, yes, no, 0, or 1) are supported.

Tcl variable

All referenced variables (in Tcl, a variable is referenced using the $ notation, for example, myVariable is a named variable, whereas $myVariable is the referenced variable).

Strings

(in double quotes)

Strings contained within double quotes may be passed with no need to include backslash, variable, or command substitution, as these are handled automatically (see the chapter on String Expressions and Handling for clarification on these terms and their usage).

Strings

(in braces)

Strings contained within braces will be used with no substitution.

Tcl commands

Tcl commands must be enclosed within square braces.

The command will be executed and the mathematical function is performed on the return value.

Named functions

Functions, such as sine, cosine, and so on.

Tcl supports a subset of the C programming language math operators and treats them in the same manner and precedence. If a named function (such as sine) is encountered, expr automatically makes a call to the mathfunc namespace to minimize the syntax required to obtain the value.

Tcl expr operators may be specified as noted in the following table, in the descending order of precedence:

Operator

Explanation

- + ~ !

Unary minus, unary plus, bitwise NOT and logical NOT.

Cannot be applied to string operands.

Bit-wise NOT may be applied to only integers.

**

Exponentiation

Numeric operands only.

*/ %

Multiply, divide, and remainder.

Numeric operands only.

+ -

Add and subtract.

Numeric operands only.

<< >>

Left shift and right shift.

Integer operands only.

A right shift always propagates the sign bit.

< > <= >=

Boolean Less, Boolean Greater, Boolean Less Than or Equal To, Boolean Greater Than or Equal To (A value of 1 is returned if the condition is true, otherwise a 0 is returned).

If utilized for strings, string comparison will be applied.

== !=

Boolean Equal and Boolean Not Equal (A value of 1 is returned if the condition is true, otherwise a 0 is returned).

eq ne

Boolean String Equal and Boolean String Not Equal (A value of 1 is returned if the condition is true, otherwise a 0 is returned).

Any operand provided will be interpreted as a string.

in ni

List Containment and Negated List Containment (A value of 1 is returned if the condition is true, otherwise a 0 is returned).

The first operand is treated as a string value, the second as a list.

&

Bitwise AND

Integers only.

^

Bitwise Exclusive OR

Integers only.

|

Bitwise OR

Integers only.

&&

Logical AND (a value of 1 is returned if both operands are 0, otherwise a 1 is returned).

Boolean and numeric (integer and floating-point) operands only.

x?y:z

If-then-else (if x evaluates to non-zero, then the return is the value of y, otherwise the value of z is returned).

The x operand must have a Boolean or a numeric value.

You have been reading a chapter from
Tcl/Tk 8.5 Programming Cookbook
Published in: Feb 2011
Publisher: Packt
ISBN-13: 9781849512985
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image