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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dont let string range fool you

Status
Not open for further replies.

smugindividual

Programmer
Apr 14, 2003
104
US
I have found this to be a problem when using string range to build lists:

The string range function will accept a range outside the bound of the string.

this command:

[string range "happy" 1 4]

would produce "appy" as expected. But these commands:

[string range "happy" -5 -1]
[string range "happy" 4 1]
[string range "happy" 10 20]
[string range "happy" 100 200]

would produce this "".


If i were working my way through a long string and was using lappend to build a list, I might slip past the last letter in the string and start filling my list with a bunch of empties (or {} as they appear in the list).

So if you have a problem with an empty string mysteriously showing up in your list, check to see if you are using string range cammand while building that list.
 
It's a very bad idea to mixt strings and lists.

Yes all is a string in Tcl and a list is a string.
But using a string command for a list is a bad idea (a string command doesn't keep the structure of a list).
Using a list command for a string needs the string to be able to be translated into a list, so to be structured as a list.

I'm not sure to be clear but my experiment is clear: it's definitivly a bad idea.

I hope that some one will give you a strange example where this is an evidence.

Maybe that:
Code:
  puts [split {set a [some command]}]
->set a {[some} command\]
ulis
 
Ohh, I know its a problem. I was posting this as a helpful hint for others. Just wanted to share the knowledge.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top