ultralord wrote:i cant unterestand the
proc filt {data} {
regsub -all -- \\\\ $data \\\\\\\\ data
regsub -all -- \\\[ $data \\\\\[ data
regsub -all -- \\\] $data \\\\\] data
regsub -all -- \\\} $data \\\\\} data
regsub -all -- \\\{ $data \\\\\{ data
regsub -all -- \\" $data \\\\" data
return $data
}
how it works? $data what var is? i must put there my $nick1 (=nick[1]) ?
thx
That filter is for avoiding double evaluation. That means before you attempt to "inject" commands into the string you run that filter over the string first. This helps protect any tcl special characters already present in the string which might get interpreted as commands. This is useful when you want to use the commands [eval] [subst] or any similar command where double-evaluation can occur. This is the reason you see so many escapes because the string is evaluated the first time, when it is turned into a string
Lets say you use this command:
set text "\[hello\]"
When the string is first evaluated we use single escapes to keep the interpreter from trying to evaluate hello as a command before it can even store anything into $text. When stored as a string these single escapes will be stripped, $text will become simply "[hello]".
But when using [subst] or [eval] over this we have a problem. The hello will be interpreted as a command because it is now being double evaluated. So the above filter can be used to keep the escapes intact when the string is stored into $text. In this way the subsequent [subst]/[eval] will not see hello as a command, but will see the escapes. And once the [subst]/[eval] evaluates the string a second time, those double evaluated escapes will also be stripped. Returning exactly the summary of the embedded commands without stumbling over any other text within.
The interpreter will evaluate three escapes \\\ back into a single escape \. The remaining will remain through each evaluation iteration, and is why you see an odd amount on the right side of the regsub, 5 of them. On the first evaluation the first three will become one. This leaves two left and combined with the one created from the first three, this again becomes three. Eggdrop likes threes.
set text [filt $text]
This is how one would make use of it, but keep in mind if you aren't doing this to avoid double evaluation but instead are using it to correct bad string/list manipulations, this can make your situation even worse.