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

Reserved characters 1

Status
Not open for further replies.

carlosAlberto

IS-IT--Management
Oct 9, 2001
109
GB
Hi,

I'm trying to write the following string to a file :

set section1 "document.form.list.options["

I think the problem lies with the ' [ '. Is there any way i can get arount this and still write the character to the file????

thanks,
 
Yes, the problem is with your "[ignore][[/ignore]" character. In Tcl, characters enclosed within [ignore][][/ignore] characters are subject to command substitution, similar to using backquotes (``) in most Unix shells. The Tcl interpreter executes whatever appears between the [ignore][][/ignore] as a Tcl command, and then replaces the character sequence with the return value of the command executed. For example:

Code:
set len [string length "Hello!"]

assigns the return value of the string length command (in this case, 6) as the value of the variable len.

There are two ways to prevent the command substitution from occuring. One is to quote your argument with {} instead of "". When an argument is quoted with "", the Tcl interpreter is allowed to do command, variable, and backslash substitutions anywhere within the argument when it parses the command line. Quoting with {} prevents the Tcl interpreter from performing any substitutions on the argument when parsing the command. So, you could simply do this instead:

Code:
set section1 {document.form.list.options[}

The other option, if you continue to use the "" quoting, is to put a backslash (\) in front of the [ignore][[/ignore]. The backslash tells the Tcl interpreter to ignore any special meaning usually assigned to the following character, and to instead treat it as a literal character. So you could fix your command this way, too:

Code:
set section1 "document.form.list.options\["

Why would you go with the second option instead of the first? Perhaps you've got an argument where you want substitutions to occur in part of the argument (perhaps variable substitutions), but not in other parts (perhaps including a literal [ignore][[/ignore]). Here's an example:

Code:
set fragment "document.form.list.options"
set section1 "$fragment\["
- Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Hi Ken,

I am facing a similar problem. I am declaring Javascript arrays in my code like:

var daysInMonth = new Array(12);
daysInMonth\[0\] = 31;
daysInMonth\[1\] = 28;
daysInMonth\[2\] = 31;

Using (\) backslash, I am able to avoid Tcl interpreter executing whatever appears between the [].

But I have to use this backslash too many times in my code lines. Is there any other option to supress these [] brackets before entering this block of code ?

A friend casually mentioned using tcl 'format' command at the beginning of the code block itself, but I can't figure out how to use it ?

Any help would be higly appreciated.

regards
vinod
+61-405 149 061
 
For a situation like this, I don't think I'd use format. The easiest thing to do will be to quote the Javascript chunk using {} characters, as described above. As I mentioned, quoting with {} prevents the Tcl interpreter from performing any substitutions on the argument when parsing the command.

As an example:

Code:
set js {var daysInMonth = new Array(12);
daysInMonth[0] = 31;   
daysInMonth[1] = 28;
daysInMonth[2] = 31;}

The only potential problem is if you need to include "{" or "}" characters in your Javascript string. If the number of "{" characters and the number of "}" characters in the string are balanced, then you'll be okay:

Code:
set c_code {for (i=0; i < 10; i++)
{
  printf(&quot;[%d]\n&quot;, i);
}}

If the braces don't balance though, it does get a bit tricky to get everything to work out okay:

[tt]% [ignore]set text {[This] is } a $text}[/ignore]
wrong # args: should be &quot;set varName ?newValue?&quot;[/tt]

Although you can use a backslash in this case to &quot;escape&quot; the &quot;}&quot;, the backslash is still treated as a literal character and included as part of the string:

[tt]% [ignore]set text {[This] is \} a $text}[/ignore]
[ignore][This] is \} a $text[/ignore][/tt]

I'll admit that situations like this aren't pretty in Tcl. You can go back to quoting with &quot;&quot; and escaping everything:

[tt]% [ignore]set text &quot;\[This\] is \} a \$text&quot;[/ignore]
[ignore][This] is } a $text[/ignore][/tt]

You can &quot;post-process&quot; the string by mapping the &quot;\}&quot; sequences to &quot;}&quot;:

[tt]% [ignore]set text {[This] is \} a text}[/ignore]
[ignore][This] is \} a text[/ignore]
% [ignore]set text [string map [list \\\} \}] $text][/ignore]
[ignore][This] is } a text[/ignore][/tt]

Or, you can build up the string piecemeal, with different quoting characters around the pieces:

[tt]% [ignore]set text {[This] is }[/ignore]
[ignore][This] is [/ignore]
% append text &quot;\} &quot;
[ignore][This] is } [/ignore]
% append text {a $text}
[ignore][This] is } a $text[/ignore][/tt]

None of these situations are fun to deal with. But I've actually never needed to handle this situation in any of the Tcl code I've written. - Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top