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.

how would i know if a variable has been set or not ?

Old posts that have not been replied to for several years.
Locked
t
tcl-idiot

how would i know if a variable has been set or not ?

Post by tcl-idiot »

im trying to check a variable if it has been set or not --

Code: Select all

proc myproc  { } {
     if { $variable == "" } { commands ... }
}
the above code gives me an error saying NO SUCH VARIABLE, true because it not been set yet but in my other procedures it's being set and i dont want to process the above procedure if the $variable have been set or already exist. Also, that $variable is dynamic (it's created at runtime)

any help would be greatly appreciated.
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

OK, first thing. If the variable is being set by other commands, then you need to make sure, it is being set globaly (IE, so it can been seen, outside of this proc command).

SO, if you want to make $variable global, then you place "global variable" ont he line below the proc line.

second, yes, it will show an error if the variable has never been set.

One way around this, is to do "set variable {}" at the beginign of the script. This will creat an instance of the variable, so the error won't occur.

Other methods include using "[info exists variable]" to test if it exists.
Locked