| View previous topic :: View next topic |
| Author |
Message |
darton Op
Joined: 21 Jan 2006 Posts: 155
|
Posted: Sat Jun 24, 2006 5:45 pm Post subject: Problem with decreasing a variable |
|
|
Hello!
In one of my scripts there is this line:
| Code: | | 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: | | 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? |
|
| Back to top |
|
 |
Garp Voice
Joined: 15 Sep 2003 Posts: 29
|
Posted: Sat Jun 24, 2006 9:17 pm Post subject: Re: Problem with decreasing a variable |
|
|
| darton wrote: | | Code: | | 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: | | set i [expr [incr i [lindex $line 0]] -1] |
|
|
| Back to top |
|
 |
DragnLord Owner

Joined: 24 Jan 2004 Posts: 711 Location: C'ville, Virginia, USA
|
Posted: Sat Jun 24, 2006 11:15 pm Post subject: |
|
|
simply:
in the script you have '[lindex $line 0]' is used to obtain the increment value |
|
| Back to top |
|
 |
darton Op
Joined: 21 Jan 2006 Posts: 155
|
Posted: Sun Jun 25, 2006 6:15 am Post subject: |
|
|
| With the '[lindex $line 0]' it is defined what should be increased. So none of your scripts work. |
|
| Back to top |
|
 |
Sir_Fz Revered One

Joined: 27 Apr 2003 Posts: 3793 Location: Lebanon
|
Posted: Sun Jun 25, 2006 7:40 am Post subject: |
|
|
| Code: | | incr i -[lindex $line 0] |
this will decrement i by '[lindex $line 0]' instead of increment. _________________ Follow me on GitHub
- Opposing
Public Tcl scripts |
|
| Back to top |
|
 |
darton Op
Joined: 21 Jan 2006 Posts: 155
|
Posted: Sun Jun 25, 2006 9:09 am Post subject: |
|
|
| 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. |
|
| Back to top |
|
 |
DragnLord Owner

Joined: 24 Jan 2004 Posts: 711 Location: C'ville, Virginia, USA
|
Posted: Sun Jun 25, 2006 9:43 am Post subject: |
|
|
if is "2"
| Code: | | 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] |
|
| Back to top |
|
 |
phab Voice
Joined: 22 Aug 2005 Posts: 12
|
Posted: Mon Jun 26, 2006 5:59 pm Post subject: |
|
|
Doenst | Code: | | set i [expr {$i - [lindex $line 0]}] | work? |
|
| Back to top |
|
 |
De Kus Revered One

Joined: 15 Dec 2002 Posts: 1361 Location: Germany
|
Posted: Tue Jun 27, 2006 3:35 am Post subject: |
|
|
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... |
|
| Back to top |
|
 |
|