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.

checking if string contains 2 the same chars

Old posts that have not been replied to for several years.
Locked
N
Nexus6
Op
Posts: 114
Joined: Mon Sep 02, 2002 4:41 am
Location: Tuchola, Poland

checking if string contains 2 the same chars

Post by Nexus6 »

Lo,

I'd like to check whether string contains 2 the same chars like "*aa*" "*bb*" and so on.

Code: Select all

if {([string match "*aa*" $string]) || ([string match "*bb*" $string])....... || ([string match"*zz*" $string])}
It would work I just wonder if is there a simplier/shorter way to code it.

TIA
T
Thor

Post by Thor »

Code: Select all


set test "abcdeefg" 
set car "" 
set last [string index $test 0]

for {set loop 1} {$loop <= [strlen $test] } {incr loop} {
   set car [string index $test $loop]
   if { $car == last } { then ..  }
   set last $car
}
User avatar
strikelight
Owner
Posts: 708
Joined: Mon Oct 07, 2002 10:39 am
Contact:

Post by strikelight »

Code: Select all

if {[regexp aa|bb|cc|dd|ee|ff|gg|hh|ii|jj|kk|ll|mm|nn|oo|pp|qq|rr|ss|tt|uu|vv|ww|xx|yy|zz $string]} { .... }
I'm sure there's yet an even more simpler way... try reading the tcl manpages for re_syntax ...
User avatar
stere0
Halfop
Posts: 47
Joined: Sun Sep 23, 2001 8:00 pm
Location: Brazil

Post by stere0 »

This doesn´t match, why? How can i use * in the search string?

set mp3players {
" \*word\*word2\* "
" \*word\*word2\* "
}

bind pubm - "*" mp3

proc mp3 {nick uhost hand chan rest} {
global botnick kick_msg mp3players
regsub -all -- {\002|\037|\026|\017|\003([0-9][0-9]?(,[0-9][0-9]?)?)?|^B|^V|^C([0-9][0-9]?(,[0-9][0-9]?)?)?|^_} $rest "" rest
foreach mp3_line $mp3players {
if {[regexp $mp3_line [string tolower [lindex [split $rest] 0]]]} {
[]s
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

THis is because of the following code.

Code: Select all

set mp3players {
" \*word\*word2\* "
" \*word\*word2\* "
}
This produces a list, which you have used in the script, but when each element is converted into a string, the escaping is lost, and it becomes a plain old *.

To get around this, I think you can use

Code: Select all

set mp3players [list]
lappend mp3players "\*word\*word2\* "
lappend mp3players "\*word3\*word4\* "
The method you use, creates the list manualy. This can be troublesome (as your script shows, it doesn't allways produce the correct results).

Lists are specialy formated, thus, when trying to manualy create the list, you have to get the exact formatting right.

Using my example above, though it is longer, it will produce the formatting for you (in theory).
Locked