skip to Main Content

In tclsh, first make an empty string:

set a []

Then run the following command:

string length a

It returns 1.

I’m expecting a return value of 0 for the length of an empty string, because it is defined to be the number of characters in the string. But I got 1. Why is that? I read this document, but still don’t understand. I’m using Tcl 8.6 on Ubuntu 22.04.

2

Answers


  1. To get the value of variable "a" in Tcl, you use $a. When you run a command like string length a, you actually request the length of the literal string "a". The length of that string is 1. To get the length of the string stored in variable "a", you must use string length $a.

    Login or Signup to reply.
  2. Also, you want:

    set a ""
    

    or

    set a {}
    

    not

    set a []
    

    The latter is an attempt to execute code, that is what ‘[‘ …’]’ means in Tcl.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search