| Author |
Adding two variables
|
|
| Steve Swift 2006-05-25, 8:14 am |
| My colleague, Mark has just got married and is on honeymoon for four
w s. I have a choice between calling his mobile and asking him, or
coming to this newsgroup and asking you. So here we are.
I've written a very simple program to teach myself how to add two
variables. Here it is:
#!./tcl3270-fix
set parts 3
puts "Parts=$parts"
set extra 4
set parts [expr $parts + $extra]
puts "Parts=$parts"
exit 0
My question is: Is the line "set parts [expr $parts + $extra]" the best
way of summing variables $parts and $extra and putting the result back
into $parts ?
It works, but prior experience with other languages led me to hope for
something like:
$parts = $parts + $extra
--
Steve Swift
http://www.ringers.org.uk
| |
| Gerald W. Lester 2006-05-25, 7:06 pm |
| Steve Swift wrote:
> My colleague, Mark has just got married and is on honeymoon for four
> w s. I have a choice between calling his mobile and asking him, or
> coming to this newsgroup and asking you. So here we are.
>
> I've written a very simple program to teach myself how to add two
> variables. Here it is:
>
> #!./tcl3270-fix
> set parts 3
> puts "Parts=$parts"
> set extra 4
> set parts [expr $parts + $extra]
> puts "Parts=$parts"
> exit 0
>
> My question is: Is the line "set parts [expr $parts + $extra]" the best
> way of summing variables $parts and $extra and putting the result back
> into $parts ?
Actually it should be:
set parts [expr {$parts + extra}]
> It works, but prior experience with other languages led me to hope for
> something like:
> $parts = $parts + $extra
Nope, you need to use the set command as above. Alternately, for this
particular case you could use the incr command as in:
incr parts $extra
--
+--------------------------------+---------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+
| |
|
|
> set parts 3
> puts "Parts=$parts"
> set extra 4
> set parts [expr $parts + $extra]
> puts "Parts=$parts"
This is okay and the normal way to handle this in Tcl!
> $parts = $parts + $extra
Such things are not possible in Tcl.
One other way to add something is:
set parts 3
puts "Parts=$parts"
set extra 4
incr parts $extra
puts "Parts=$parts"
Regards,
Sascha
| |
| Cameron Laird 2006-05-25, 7:06 pm |
| In article <BVidg.72854$IZ2.5942@dukeread07>,
Gerald W. Lester <Gerald.Lester@cox.net> wrote:
>Steve Swift wrote:
| |
| Ralf Fassel 2006-05-25, 10:03 pm |
| * "eiji" <SaLeuth@gmx.de>
| One other way to add something is:
--<snip-snip>--
| incr parts $extra
Ob-Nitpick: 'parts' and 'extra' have to be plain integers, otherwise
'incr' will raise an error.
% set parts 1
1
% set extra 1
1
% incr parts $extra
2
% set extra 1.1
1.1
% incr parts $extra
expected integer but got "1.1"
For adding arbitrary numbers, use 'expr'.
R'
| |
| Steve Swift 2006-05-25, 10:03 pm |
| > incr parts $extra
Thank you all your your help and reasoned explanations. I'll probably go
with the "incr" statement.
Who, knows, I may have something to impress Mark with when he gets back
from his honeymoon!
--
Steve Swift
http://www.ringers.org.uk
| |
| Steve Swift 2006-05-25, 10:03 pm |
| > Ob-Nitpick: 'parts' and 'extra' have to be plain integers, otherwise
> 'incr' will raise an error.
Well in this case we're dealing with counts of spare parts for repairing
IBM machines in the field, so one hopes they come in integer increments!
:-)
--
Steve Swift
http://www.ringers.org.uk
| |
| Gerald W. Lester 2006-05-25, 10:03 pm |
| Steve Swift wrote:
>
> Well in this case we're dealing with counts of spare parts for repairing
> IBM machines in the field, so one hopes they come in integer increments!
> :-)
I don't know, I've seen half of a circuit board in my day 8-}
Of course it was normally the part to be repaired rather then the spare, but...
--
+--------------------------------+---------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+
| |
| Larry Smith 2006-05-25, 10:03 pm |
| Steve Swift wrote:
> My question is: Is the line "set parts [expr $parts + $extra]" the best
> way of summing variables $parts and $extra and putting the result back
> into $parts ?
> It works, but prior experience with other languages led me to hope for
> something like:
> $parts = $parts + $extra
http://wiki.tcl.tk/2696
--
..-. .-. .---. .---. .-..-.|Experts in Linux: www.WildOpenSource.com
| |__ / | \| |-< | |-< > / |"Making the bazaar more commonplace"
`----'`-^-'`-'`-'`-'`-' `-' |Check out my new novel: "Cloud Realm" at:
home:www.smith-house.org:8000|<-this url + /books/list.html
| |
|
| In article <1148566522.045280.169770@j55g2000cwa.googlegroups.com>,
eiji <SaLeuth@gmx.de> wrote:
>
>
>This is okay and the normal way to handle this in Tcl!
>
>
>Such things are not possible in Tcl.
NEVER say "not possible" to a Tcl hacker :-)
I believe somewhere on the wiki is something that defines "proc =" to do the
right thing..
MH
| |
|
|
| Cameron Laird 2006-05-26, 7:06 pm |
| In article <4478247c@news.greennet.net>,
Steve Swift <Steve_Swift@uk.ibm.com> wrote:
>
>That's very clever, and thanks for pointing it out, but that "let" proc
>would swamp my code, I think.
|
|
|
|