This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

special char

Help for those learning Tcl or writing their own scripts.
Post Reply
User avatar
ultralord
Master
Posts: 255
Joined: Mon Nov 06, 2006 6:52 pm

special char

Post by ultralord »

hello.. i have one problem.. i have one tcl script with mysql etc.. when i add one nick in one table and nick have [ ] the problem is when i export the table on php i saw the nick with { } like..

nick[1] and i see it {nick[1]} can i escape the [] and { } from any nick?

i saw here something
http://forum.egghelp.org/viewtopic.php? ... characters with that command

regsub -all -- {(\\|\[|\]|\&|\{|\}|"|'|\262|\263)} $data {\\\1} data

but i dont know where i must put.. on proc who read the nick or on the tcl anywhere?

thanks a lot..
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

The presence of {} surrounding strings containing [] usually indicates you're actually dealing with a list.

Think you could post the code in question, would help us see if you really need that regsub, and where to put it should you do...
NML_375
t
tsukeh
Voice
Posts: 31
Joined: Thu Jan 20, 2005 6:22 am

Post by tsukeh »

User avatar
ultralord
Master
Posts: 255
Joined: Mon Nov 06, 2006 6:52 pm

Post by ultralord »

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
User avatar
speechles
Revered One
Posts: 1398
Joined: Sat Aug 26, 2006 10:19 pm
Location: emerald triangle, california (coastal redwoods)

Post by speechles »

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.
User avatar
ultralord
Master
Posts: 255
Joined: Mon Nov 06, 2006 6:52 pm

Post by ultralord »

thnx i find my solution set nick1 [ join $nick1 ]

ty.
Post Reply