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.

trouble with a nick containing [ ]

Help for those learning Tcl or writing their own scripts.
Post Reply
User avatar
CrazyCat
Revered One
Posts: 1334
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

trouble with a nick containing [ ]

Post by CrazyCat »

I know this is a recurent trouble but I can't find the solution, I've tried all my ideas.

I'm trying to exlude some users in a procedure (some bots) and their nick is [nick]
So, I do:

Code: Select all

proc test {nick uhost handle chan args} {
   set exclbot "\[nick1\] \[nick2\]"
   set exclude [split "$::botnick $exclbot"]
   set users [chanlist $chan]
   foreach excl [split $exclude] {
      set ind [lsearch $users $excl]
      putlog "$excl - $users => $ind"
      set users [lreplace $users $ind $ind]
   }
}
When the tcl found [nick1] or [nick2], the lsearch returns -1 and I've in the log:

Code: Select all

{[nick1]} - {[nick1]} {[nick2]} => -1
I can't understand why the result is not 0... it works well with others users...
User avatar
CrazyCat
Revered One
Posts: 1334
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Post by CrazyCat »

I've found a method I used previously: a filtering procedure:

Code: Select all

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
}
I don't really like it, I'm sure a real solution exists...
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Keep in mind that lsearch uses glob-style patterns unless you specify otherwize.

Have you considdered using proper lists? Right now, you're splitting lists, which can damage contents.

Also, you really should check for the condition where the excluded nick was not found (-1 returned)...

Code: Select all

proc test {nick uhost handle chan arg} {
 set exclude [list $::botnick {[nick1]} {[nick2]}]
 set users [chanlist $chan]
 foreach excl $exclude {
  if {[set ind [lsearch -exact $users $excl]] >= 0} {
   putlog "$excl - $users => $ind"
   set users [lreplace $users $ind $ind]
  }
 }
}
NML_375
User avatar
CrazyCat
Revered One
Posts: 1334
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Post by CrazyCat »

The trouble I've is that the list of excluded nicks is a configuration setting of the tcl, I can't ask users to use something different than the name the see on IRC.

Bu I'll try the lsearch with options like -exact and -ascii, peharps this is the solution :)
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

The glob-style patternmatching is the cause of your issues, so adding -exact will solve that issue.

Yet, you should'nt use split over and over again...

Something like this then, perhaps?

Code: Select all

...
 set exclude $::excludednicks
 lappend exclude $::botnick
...
NML_375
User avatar
CrazyCat
Revered One
Posts: 1334
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Post by CrazyCat »

I'll try this tomorrow and give a report here, it might be an add to the FAQ about strings.
Post Reply