Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

empty string inside a function 1

Status
Not open for further replies.

amirvosko

Programmer
Apr 19, 2005
7
US
Can anyone explain what happend to an empty string ( "" ) when passed as parameter into a function.

I'm trying to do the TCL equivalnt for the C code :

void foo(const string & str ) {
if (str == "") {
cout << "Eurika";
}
}


What I see is that in the Tcl function the empty string is converted to \{\} # a 2 char string of pair of curely brackets


func foo {str} {
puts "string - $str"
puts "length - [string length $str]"
puts "char 0 - [string index $str 0 ]"
puts "char 1 - [string index $str 1 ]
if {$str == "" } { puts "Eurika" }
if {$str == "\{\}" } { puts "Why " }
}

> foo ""
string - {}
length - 2
char 0 - {
char 1 - {
Why


# Running foo {} or foo \{\} gives the same
???



 
Tcl uses this "", as list notation as well as {}.
For your purposes however you are going about it
the wrong way.

Code:
52 % proc str {{thing ""}} {
                      return $thing
}
53 % if {[string compare [str] ""] ==0} {puts "EUREKA"}
EUREKA
 
Your TCL Interpreter shows some strange behaviour there. First of all, an empty string is NOT converted to curly braces ({}). Only when using puts, empty list elements are represented this way -> {}.

If I run your program using
> foo ""

the result is:
string -
length - 0
char 0 -
char 1 -

Same goes for
> foo {}

Since, as mentioned before by marsd, {} and "" are synonyme syntax for an empty string/list/list element.

WHILE:
foo \{\}

really passes a two-char string to the function.

Greetings,
Tobi
 
You are right it is a specific problem to our in house parser that comes on top of the standart TCL parser. In the original TCL "proc" command (vs our "func" derivative of it) their indeed no problem and the empty string/list is past into the proc as I would expected. Our "func" command parse the parameters in its own way, convert the "" and than regardless whats the script is (string compare/ string index etc ) the argument is wrong and we are already dead
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top