tags:

views:

253

answers:

3

I have been playing around in expect recently and I for the life of me can't figure out how to perform a simple addition on a variable I passed in. Anyone know how to do this? Also, is there a decent online reference for Expect? I have tried googling with very limited results.

A: 

I would start here at the official website.

NoahD
I would, but there really isn't any real resources there, just a recommendation to buy the book Exploring Expect.
Craig H
and an excellent recommendation it is: it's one of the best-written programming books around
glenn jackman
A: 

Ahh, ok, I figured it out:

set count [expr $count+1]

This adds 1 to the count variable.

Craig H
+6  A: 

The thing to remember about Except is that it's really just an extension to Tcl, so if you are looking for help on writing Expect scripts and your question is not related to one of the Expect specific commands, you should try looking in the Tcl references. A good starting place is http://www.tcl.tk, as well as http://wiki.tcl.tk.

There are two ways to do what you're trying to do: incr and expr. incr may be used when you are adding an integer value to another integer. It is very fast for that operation. For example:

set value 1
incr value

However, incr does not work with non-integer values, and it can't do anything but addition (or subtraction if you negate the increment, as in incr value -1). If you need something more elaborate, you should use expr:

set value 1
set value [expr {$value + 1}]

Note the use of curly braces around the expression! Although they are not required for correct operation in general, they improve performance. If you are doing many arithmetic operations, using braces around the expressions will significantly improve the performance of your script. For more information, see http://wiki.tcl.tk/10225. You should get in the habit of always bracing your expressions when using expr.

You can find links to several Expect resources at http://wiki.tcl.tk/201.

Eric Melski