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.

detect nicks with excessive digits in it

Requests for complete scripts or modifications/fixes for scripts you didn't write. Response not guaranteed, and no thread bumping!
Post Reply
s
simo
Revered One
Posts: 1071
Joined: Sun Mar 22, 2015 2:41 pm

detect nicks with excessive digits in it

Post by simo »

is it possible to have tcl check for digits like more than 6 digits anywhere in the nick
Last edited by simo on Sun Mar 03, 2019 12:25 pm, edited 1 time in total.
User avatar
caesar
Mint Rubber
Posts: 3776
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Code: Select all

if {[string length [regsub -all {[^0-9]} $nick ""]] > 6} {
	# do whatever
}
Once the game is over, the king and the pawn go back in the same box.
s
simo
Revered One
Posts: 1071
Joined: Sun Mar 22, 2015 2:41 pm

Post by simo »

tested it and works fine thnx caesar
s
simo
Revered One
Posts: 1071
Joined: Sun Mar 22, 2015 2:41 pm

Post by simo »

its old topic but i tried this one again and it seems it counts general chars lenght and not the amount of digits anywhere in the nick
s
simo
Revered One
Posts: 1071
Joined: Sun Mar 22, 2015 2:41 pm

Post by simo »

i tried this

Code: Select all

	if {[regexp -all {[0-9]} $nick] > 6} {
       do whatever here
}
it does seem to work not sure if its proper and not sure if its fastest ? since from what i keep hearing is regexp might not be the fastest to process
User avatar
caesar
Mint Rubber
Posts: 3776
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

The code you mentioned returns the digits from the string, not it's length, hence why i suggested the initial code:

Code: Select all

if {[string length [regsub -all {[^0-9]} $nick ""]] > 6} {
   # do whatever
}
that removes any character that isn't in the 0 to 9 range and then returns it's length.

Anyway, there are 3 options to get the digits from a string:

1. regsub
2. regexp
3. split, loop and compare

Code: Select all

% set text "f2A94d"
f2A94d

% regsub -all {[^0-9]} $text ""
294

% join [regexp -all -inline -- {[0-9]+} $text] ""
294

% join [regexp -all -inline -- {\d+} $text] ""
294

% foreach e [split $text ""] {
        if {[string is integer $e]} {
                append result $e
        }
}
% puts $result
294
Now, if you want to see who's faster, or if you wonder if that regsub is slow:

Code: Select all

% time { regsub -all {[^0-9]} $text "" } 1000
3.553 microseconds per iteration

% time { join [regexp -all -inline -- {\d+} $text] "" } 1000
3.457 microseconds per iteration
% time { join [regexp -all -inline -- {[0-9]+} $text] "" } 1000
3.939 microseconds per iteration

% time { foreach e [split $text ""] { if {[string is integer $e]} { append result $e } } } 1000
10.051 microseconds per iteration
% time { foreach e [split $text ""] { if {[string is integer $e]} { append result $e } } } 1000
8.604 microseconds per iteration
% time { foreach e [split $text ""] { if {[string is integer $e]} { append result $e } } } 1000
9.156 microseconds per iteration
% time { foreach e [split $text ""] { if {[string is integer $e]} { append result $e } } } 1000
6.549 microseconds per iteration
% time { foreach e [split $text ""] { if {[string is integer $e]} { append result $e } } } 1000
9.37 microseconds per iteration
Right off the bat we see two possible candidates, so we try again with 10000 times:

Code: Select all

% time { regsub -all {[^0-9]} $text "" } 10000
2.5413 microseconds per iteration
% time { join [regexp -all -inline -- {\d+} $text] "" } 10000
2.7552 microseconds per iteration
Hope this answers your question. :)

Edit: We can use scan instead of string is integer

Code: Select all

% foreach e [split $text ""] {
        if {[scan $e {%d} no] > 0} {
                append result $e
        }
}
% puts $result
294
but given that it's a loop will still take a lot longer than regsub or regexp:

Code: Select all

% time { unset result; foreach e [split $text ""] { if {[scan $e {%d} no] > 0} { append result $e} } } 1000
11.501 microseconds per iteration
% time { unset result; foreach e [split $text ""] { if {[scan $e {%d} no] > 0} { append result $e} } } 1000
13.495 microseconds per iteration
% time { unset result; foreach e [split $text ""] { if {[scan $e {%d} no] > 0} { append result $e} } } 1000
13.228 microseconds per iteration

Edit: Actually the code you tried:

Code: Select all

   if {[regexp -all {[0-9]} $nick] > 6} {
       #do whatever here
}
returns the length of digits from 0 to 9 it finds in $nick variable. Duno at what was looking at when did the initial test. /facepalm
Last edited by caesar on Mon Jun 22, 2020 1:22 am, edited 1 time in total.
Once the game is over, the king and the pawn go back in the same box.
s
simo
Revered One
Posts: 1071
Joined: Sun Mar 22, 2015 2:41 pm

Post by simo »

that removes any character that isn't in the 0 to 9 range and then returns it's length.
but we wanted to count the digits in the nick to check for amount of digits

as this seems to count the length of the nick and not the amount of digits in it:

Code: Select all

if {[string length [regsub -all {[^0-9]} $nick ""]] > 6} {
   # do whatever
}
User avatar
caesar
Mint Rubber
Posts: 3776
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Code: Select all

% set nick "f2A94d"
f2A94d
% regsub -all {[^0-9]} $nick ""
294
% string length [regsub -all {[^0-9]} $nick ""]
3
% set nick "aaaa3db4"
aaaa3db4
% string length [regsub -all {[^0-9]} $nick ""]
2
isn't this what you where looking for?
Once the game is over, the king and the pawn go back in the same box.
s
simo
Revered One
Posts: 1071
Joined: Sun Mar 22, 2015 2:41 pm

Post by simo »

im not sure what that does ceasar but what we was looking for was to count digits in nick and if exceeding like 6 or higher to take action
s
simo
Revered One
Posts: 1071
Joined: Sun Mar 22, 2015 2:41 pm

Post by simo »

tested it again seems to work now
User avatar
CrazyCat
Revered One
Posts: 1216
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Post by CrazyCat »

simo wrote:im not sure what that does ceasar but what we was looking for was to count digits in nick and if exceeding like 6 or higher to take action
What does ceasar is quite simple: he replaces all characters which are not between 0 to 9 with nothing and count the length of the resulting string.
s
simo
Revered One
Posts: 1071
Joined: Sun Mar 22, 2015 2:41 pm

Post by simo »

Tnx crazycat after taking a look again i saw its working and its exactly what was desired another fine job from caesar tnx again caesar much appreciated as always
Post Reply