string - Tcl: default if variable is empty? -


i've "inherited" tcl code, , while worked through tutorials , can make sense out of language, own tcl constructs lack certain... finesse.

for example, have code:

puts "column 'name': [ $queryrs getstring name ]" 

$queryrs result set of sql query. [ $queryrs getstring name ] construct retrieves content of table column "name" current row in result set. if database field null, puts not print anything.

i print "default" string instead, i.e. if [ $queryrs getstring name ] results in nothing, i'd replace "--".

now, this:

set namevar "[ $queryrs getstring name ]" if { [ string length $namevar ] == 0 } {     set namevar "--" } puts "column 'name': $namevar" 

but there has more compact solution, can done inline instead of adding 4 lines , temporary variable. help, please?

two things.

first, tcl doesn't have notion of null (nil, undefined or whatever) value, , when want simulate such value have either use variable , test existence or use entry in array of dictionary , test existence, too. if such variable/entry exists, value defined, otherwise it's not.

unfortunately, creator of inherited code apparently did not care case variable can null nulls indistinguishable variables having default values (an empty string).

next, can use helper procedure need:

proc valueordef {val {def --}} {    expr {$val ne "" ? $val : $def} } 

and go this:

puts [valueordef [$queryrs getstring name]] puts [valueordef [$queryrs getstring name] "some other default"] 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -