For Programmers: Free Programming Magazines  


Home > Archive > Tcl > May 2006 > Adding two variables









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

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
ws. 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
> ws. 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|
+------------------------------------------------------------------------+
eiji

2006-05-25, 7:06 pm


> 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
MH

2006-05-25, 10:03 pm

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
Steve Swift

2006-05-26, 4:10 am

> http://wiki.tcl.tk/2696

That's very clever, and thanks for pointing it out, but that "let" proc
would swamp my code, I think.

--
Steve Swift
http://www.ringers.org.uk
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.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com