set word "eggdrop"
proc get:stuff { stuff } {
global word
set first3letters [string range $word first 2]
# returns egg
set firstletter [string range $word first 0]
# returns e
if {[regexp {[0-9]} $firstletter]} {return "letter is a number silly"}
}
I once was an intelligent young man, now i am old and i can not remember who i was.
user wrote:regexp SUCKS (so slow compared to most string commands that perform such tasks...it's only needed for really complex patterns that would require lots of other commands)
Here's an example (matching a number as the first char in a string):
% set a 0abcdefghijklmnopqrstuvwxyz
0abcdefghijklmnopqrstuvwxyz
% time [list regexp {[0-9]+.*} $a] 100000
10 microseconds per iteration
% time [list string match {[0-9]*} $a] 100000
2 microseconds per iteration
For the record, it might help a little bit if you used the correct regexp
Instead of having it search the string anywhere for a number ;x
Also, I can not stress enough the importance of writing compatible code for various TCL versions, and Eggdrop versions... While string match is faster, it does not support regular expression matching in all TCL versions. Keeping this in perspective, 5-6 MICROseconds are inconsequential, for the price of retaining backwards compatibility.
strikelight wrote:For the record, it might help a little bit if you used the correct regexp
Yeah (It wouldn't matter much to the speed difference though)
strikelight wrote:Also, I can not stress enough the importance of writing compatible code for various TCL versions, and Eggdrop versions... While string match is faster, it does not support regular expression matching in all TCL versions. Keeping this in perspective, 5-6 MICROseconds are inconsequential, for the price of retaining backwards compatibility.
What do you mean? You're coding for some version < 7.5?
And for the record, microseconds matter when you execute stuff millions of times
user wrote:regexp SUCKS (so slow compared to most string commands that perform such tasks...it's only needed for really complex patterns that would require lots of other commands)
Here's an example (matching a number as the first char in a string):
% set a 0abcdefghijklmnopqrstuvwxyz
0abcdefghijklmnopqrstuvwxyz
% time [list regexp {[0-9]+.*} $a] 100000
10 microseconds per iteration
% time [list string match {[0-9]*} $a] 100000
2 microseconds per iteration
ok, do these two examples (regexp and string) do exactly the same thing ?
like I can convert any regexp to a string on the same concept ?
Sir_Fz wrote:ok, do these two examples (regexp and string) do exactly the same thing ? like I can convert any regexp to a string on the same concept ?
No. Use {^[0-9]} in the regexp to match the same thing as {[0-9]*} using string match.
Most regexp rules CAN'T be translated into string match masks, but when they can and become alot more efficient, they should be IMO. (Read the manual to find out what you can and can't)