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.

Replacing spaces with %20

Old posts that have not been replied to for several years.
S
Sc0rp
Voice
Posts: 11
Joined: Tue Sep 09, 2003 5:06 pm

Replacing spaces with %20

Post by Sc0rp »

Ive written a little tcl script that gets the content of a php page Ive set..

everything works fine untill the 'thing' (username in this case) I want to retreive has spaces in it..
cus then the bot somehow replaces the space with a character which the webserver does not understand..
so.. what I want to do is replace a space with %20 which is a space for internet browsers when used in the address.. (duh :))
problem is im kinda new at tcl scripting and I dont know how to replace stuff..

so my question is HOW can I replace a space with %20 :)
User avatar
strikelight
Owner
Posts: 708
Joined: Mon Oct 07, 2002 10:39 am
Contact:

Re: Replacing spaces with %20

Post by strikelight »

Sc0rp wrote:Ive written a little tcl script that gets the content of a php page Ive set..

everything works fine untill the 'thing' (username in this case) I want to retreive has spaces in it..
cus then the bot somehow replaces the space with a character which the webserver does not understand..
so.. what I want to do is replace a space with %20 which is a space for internet browsers when used in the address.. (duh :))
problem is im kinda new at tcl scripting and I dont know how to replace stuff..

so my question is HOW can I replace a space with %20 :)

Code: Select all

regsub -all " " $text "%20" text
or

Code: Select all

set text [string map {" " "%20"} $text]
I prefer the first method myself..

also, a better place to start before asking for commands, is the tcl manpages.
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Code: Select all

set text [join [split $a " "] %20]
would also work (almost the same as string map cpu-wise and compatible with all tcl versions)

The nice thing about join/split and string map is that they return the result, so there's no need to do the command invocation and then read the value. The nice thing about regsub is that you'll know how many replacements were made and you'll keep your cpu less idle. :)

btw: I bet you could use + instead of %20 and save some space. (heh)
User avatar
strikelight
Owner
Posts: 708
Joined: Mon Oct 07, 2002 10:39 am
Contact:

Post by strikelight »

user wrote:

Code: Select all

set text [join [split $a " "] %20]
would also work (almost the same as string map cpu-wise and compatible with all tcl versions)

The nice thing about join/split and string map is that they return the result, so there's no need to do the command invocation and then read the value. The nice thing about regsub is that you'll know how many replacements were made and you'll keep your cpu less idle. :)

btw: I bet you could use + instead of %20 and save some space. (heh)

Code: Select all

% time {set newtext [join [split $text " "] %20]} 100000
33 microseconds per iteration
% time {set newtext [string map {" " %20} $text]} 100000
14 microseconds per iteration
% time {regsub -all " " $text "%20" newtext} 100000
15 microseconds per iteration
% time {set newtext [join [split $text " "] %20]} 100000
30 microseconds per iteration
Are we ever going to stop debating this? :D
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Interesting numbers...what tcl version? My tests in 8.3.2 were quite different (string map and split/join about the same, regsub 4-5 times slower)
strikelight wrote:Are we ever going to stop debating this? :D
I hope not :P

Try to guess what my first edit was.
Last edited by user on Tue Sep 09, 2003 6:59 pm, edited 2 times in total.
Have you ever read "The Manual"?
User avatar
strikelight
Owner
Posts: 708
Joined: Mon Oct 07, 2002 10:39 am
Contact:

Post by strikelight »

user wrote:Interesting numbers...what tcl version? My tests in 8.3.2 were quite different (string map and split/join about the same, regsub 4-5 times slower)

Code: Select all

% info patchlevel
8.4.1
% puts "$tcl_platform(os) $tcl_platform(osVersion)"
Windows NT 5.0
% 
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

Both "string map" and the split-join method can also count many many replacments where done.

Code: Select all

set len [string length $orig]
set ph "%20"
set new [join [split $orig] $ph]
set count [expr ([string length $new] - $len) / [string length $ph]]
Not as simple or elegant, more CPU intensive.

As for the battle of time, this will vary on text length and system. The battle between Windows and Linux has allways been odd.

Windows has shown on occasions to beat the pants of linux in some commands, but lagged in others. Linux shows consistant replies where windows hasn't.

Note, there was one test missed out.

Code: Select all

set text [string repeat "ssss " 30]
time {set newtext [join [split $text " "] %20]} 1000
time {set newtext [string map {" " %20} $text]} 1000
time {regsub -all " " $text "%20" newtext} 1000
time {set newtext [join [split $text] %20]} 1000
The results in order (cycles reduced)

Code: Select all

92 microseconds per iteration
108 microseconds per iteration
405 microseconds per iteration (ouch)
110 microseconds per iteration
note how using the default marker for "split" rather than " " causes time to increase.

Here are the tests again, with the "string repeat" set to 10

Code: Select all

46 microseconds per iteration
47 microseconds per iteration
151 microseconds per iteration
53 microseconds per iteration
Tcl 8.3.4 - Linux 2.4.21

System level optimisations are not important to a coder, generic speed is. Unless they are gonna use a test suite first to determine the best command for the job.
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

may I ask, how do you do these tests ? on what basis do you get the time results ?
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

Tcl provides a command called "time".

This will measure the time in microseconds, it takes to complete a command.

You also give it a repeat counter. This will loop and call the command X times. The total time is added together, and devided by the loop value, to give an average time.

Tcl also provides a command line parser, called "tclsh". You can use this to run Tcl script in your everyday tasks, or even as a CGI library. When called on its own, you can use it interactivly.

Through this, you can perform these tests, though you can't test eggdrop scripts, as many of the required commands are not available.
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

can you give an example please ? using one of the commands above :P
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

Examples have allready been given.

Code: Select all

time {set test "hello"} 1050
This will run the code

Code: Select all

set test "hello"
1050 times, and tell you how long on average it took to run the command each time.

It will display somthing like
2 microseconds per iteration
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

I tried ".tcl time {set test "hello"} 1050" in partyline, it returned: Tcl error: wrong # args: should be "time". what's wrong ?
e
egghead
Master
Posts: 481
Joined: Mon Oct 29, 2001 8:00 pm
Contact:

Post by egghead »

Sir_Fz wrote:I tried ".tcl time {set test "hello"} 1050" in partyline, it returned: Tcl error: wrong # args: should be "time". what's wrong ?
Good point. It seems eggdrop has its own definition of time. Could not find it in doc/tcl-commands.doc though.

In any case, you can start tcl on your shell:
system:/usr/home/egghead # tclsh8.3
% time {set test "hello"} 1050
1 microseconds per iteration
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

okay thanx egghead :) I've got it, but may I ask how does the time vary according to how many time a command is repeated.

because when I do "time {set test "hello"} 5" it returns 4 microseconds per iteration ; and when I do "time {set test "hello"} 1050" it returns 1 microseconds per iteration.

so why is it less time to repeat 1050 times than 5 times ?
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

egghead wrote:Good point. It seems eggdrop has its own definition of time. Could not find it in doc/tcl-commands.doc though.
It's not been part of eggdrop for a while. But there's a proc called "time" in scripts/compat.tcl.
Locked