This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Problem with decreasing a variable

Help for those learning Tcl or writing their own scripts.
Post Reply
d
darton
Op
Posts: 155
Joined: Sat Jan 21, 2006 11:03 am

Problem with decreasing a variable

Post by darton »

Hello!
In one of my scripts there is this line:

Code: Select all

incr i [lindex $line 0]
But now I don't want to increase it but I want to decrease it. So I changed it to this:

Code: Select all

incr i [lindex $line 0] -1
It does not work. The error is: Tcl error [flag]: wrong # args: should be "incr varName ?increment?"
What's wrong?
G
Garp
Voice
Posts: 29
Joined: Mon Sep 15, 2003 7:58 pm

Re: Problem with decreasing a variable

Post by Garp »

darton wrote:

Code: Select all

incr i [lindex $line 0] -1
you do something like "1 +1 -1" but incr allows only one operation like "1+1" or "1-1"

(($i + $y) - $z) it would be something like

Code: Select all

set i [expr [incr i [lindex $line 0]] -1]
User avatar
DragnLord
Owner
Posts: 711
Joined: Sat Jan 24, 2004 4:58 pm
Location: C'ville, Virginia, USA

Post by DragnLord »

simply:

Code: Select all

incr i -1
in the script you have '[lindex $line 0]' is used to obtain the increment value
d
darton
Op
Posts: 155
Joined: Sat Jan 21, 2006 11:03 am

Post by darton »

With the '[lindex $line 0]' it is defined what should be increased. So none of your scripts work.
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

Code: Select all

incr i -[lindex $line 0]
this will decrement i by '[lindex $line 0]' instead of increment.
d
darton
Op
Posts: 155
Joined: Sat Jan 21, 2006 11:03 am

Post by darton »

Yes, it works Sir_Fz. But if i is 3 and it should be decreased the result is 2. With your script it is not 2, but -2. With "trimleft" I can make the variable i positive again, but maybe this is too circumstantial and there is an easier way.
User avatar
DragnLord
Owner
Posts: 711
Joined: Sat Jan 24, 2004 4:58 pm
Location: C'ville, Virginia, USA

Post by DragnLord »

if

Code: Select all

[lindex $line 0]
is "2"

Code: Select all

incr i -[lindex $line 0]
means that it is increased by -2, which means it is decreased by 2
obviously you didn't bother to try it[/code]
p
phab
Voice
Posts: 12
Joined: Mon Aug 22, 2005 6:34 am

Post by phab »

Doenst

Code: Select all

set i [expr {$i - [lindex $line 0]}]
work?
User avatar
De Kus
Revered One
Posts: 1361
Joined: Sun Dec 15, 2002 11:41 am
Location: Germany

Post by De Kus »

its equivalent with 'incr i -[lindex $line 0]', however only the expr one will work on negative values.
De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens...
Post Reply