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.

proc decr as incr in tcl - input an expression in proc

Old posts that have not been replied to for several years.
Locked
c
cerberus_gr
Halfop
Posts: 97
Joined: Fri Feb 07, 2003 8:57 am
Location: 127.0.0.1

proc decr as incr in tcl - input an expression in proc

Post by cerberus_gr »

Hello,

1)
I want to write a procedure decr as incr command in tcl.

I have already done this:

Code: Select all

proc decr { var } { set ${var} "[expr ${var} - 1]" }
but it stops with error:
Tcl error: can't read "$var": no such variable

set variable_name variable_value
variable_name = ?
variable_value = ?

2)
I want to have a procedure which has as input an expression

examples:
proc chk { expr } { if {$expr} { return "true" } }

[chk "1 = 1"] -> true
[chk "string match a hsagsa'] -> true
[chk "3 >= 4"] -> null

but my code just checks if $expr is/isn't " "

Any help?
Last edited by cerberus_gr on Thu Nov 27, 2003 11:37 am, edited 2 times in total.
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

try $var instead of ${var}
c
cerberus_gr
Halfop
Posts: 97
Joined: Fri Feb 07, 2003 8:57 am
Location: 127.0.0.1

Post by cerberus_gr »

$var will return the name of the variable and not it's value

if ($hello == 10) then "decr hello" will set var = hello and not var = 10
I want to "read" bot name and value, $var returns the name, but HOW could I take the value?
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

your code should be:

Code: Select all

proc decr { var } { set var "[expr $var -1]" }
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

1) To work with the actual variable you'll have to import it using 'upvar' or execute the expr/incr in the scope your variable exists in using 'uplevel'

Code: Select all

proc decr var {
	upvar 1 $var bla
	incr bla -1
}
...or:

Code: Select all

proc decr var {
	uplevel 1 [list incr $var -1]
}
2) use 'expr':

Code: Select all

proc chk expr {
	if {[expr $expr]} {return "true"} {return "null"}
}
...or use the C-style "if-then-else" structure:

Code: Select all

proc chk expr {
	expr $expr?"true":"null"
}
...but then you might aswell use 'expr' directly where you want the check imo. Oh, and btw: "1 = 1" is not a valid expression in tcl and the string "null" doesn't mean anything speacial.
Have you ever read "The Manual"?
c
cerberus_gr
Halfop
Posts: 97
Joined: Fri Feb 07, 2003 8:57 am
Location: 127.0.0.1

Post by cerberus_gr »

Thx a lot both :)

1) user I haven't taught tcl and I know only a few commands and I didn't know that in incr command u would use numbers < 0, so my problem has sold

2) Perfect

P.S. in "1 = 1" expression I forgot one more "=" :) and in my code I didn't returned anything, so that's why I wrote null :) Thanks a lot :)
Locked