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.

using list in a foreach command

Old posts that have not been replied to for several years.
S
Solarin
Voice
Posts: 29
Joined: Wed Apr 09, 2003 1:50 pm
Location: Sydney Australia
Contact:

using list in a foreach command

Post by Solarin »

Take the following code excerpts as an example:

Code: Select all

set cv_bannednicks {
 [xxx]
}

.
.
.

  foreach s $cv_bannednicks {
   if {[lsearch -glob [string tolower $nick] "*$s*"] != -1} {
.
.
.
shouldn't I be using:

Code: Select all

list foreach s $cv_bannednicks {
???

using:

Code: Select all

foreach s [list $cv_bannednicks] {
does not work
and I am using the list command to tell the interpreter to handle special characters in the list $cv_bannednicks.

Can anyone shed some light on this?

Furthermore, if I were to use a list with items that must include spaces. Why doesn't enclosing them in quotes work?

Code: Select all

set cv_bannedversions {
 irc-ord
 "xdcc catcher basic"
User avatar
De Kus
Revered One
Posts: 1361
Joined: Sun Dec 15, 2002 11:41 am
Location: Germany

Post by De Kus »

usually you use split, in your case probably you need after definition.
set cv_bannednicks [split cv_bannednicks \n]
however, this will retain all white spaces, so maybe you should make your list like:

set cv_bannednicks {firsttag
[tag2]
{tag3}
$tag4$
lasttag}

btw. you dont need to use "", if you use split on \n.

ps: you should use [string match -nocase *$s* $nick] instead of lsearch, because $nick isnt supposed to be a list... however it is a string as it is, if you used [split $nick], you would change it into a 1 element list.
De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens...
S
Solarin
Voice
Posts: 29
Joined: Wed Apr 09, 2003 1:50 pm
Location: Sydney Australia
Contact:

Post by Solarin »

Code: Select all

 set nick "[lindex [split $args " "] 6]" 
is how I'm setting nick btw ;) Not your ordinary proc I can assure you.

Thanks for the advice on splitting on \n. I will give that a whirl.
Note that it should be:

Code: Select all

set cv_bannednicks [split $cv_bannednicks \n]
In regards to:

Code: Select all

list foreach s $cv_bannednicks { 
is that properly done?

Something else I came across:

Code: Select all

set tcl_precision 2
set percent [expr $cv_banned/$cv_count*100.00]
putserv "PRIVMSG $chan :Banned $cv_banned hosts out of $cv_count clients ($percent%). 
That snippet is showing the following text in channel:
Banned 40 hosts out of 457 clients (0.0%).

Why 0.0%?
User avatar
awyeah
Revered One
Posts: 1580
Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:

Post by awyeah »

You can set your list like:

Code: Select all

set mylist {
"element1"
"element2"
"element3"
"element4"
"element5"
}

#and then do the matching:

foreach element $mylist {
 if {[string match -nocase *$element* $nick]} {
  #do your stuff
·­awyeah·

==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Don't forget to close the if and the foreach loop.
Once the game is over, the king and the pawn go back in the same box.
S
Solarin
Voice
Posts: 29
Joined: Wed Apr 09, 2003 1:50 pm
Location: Sydney Australia
Contact:

Post by Solarin »

Nobody can shed light on my other questions?

Please remember I'm dealing with nicks here. I could have nicks with special characters like { or [.

Also note that I used the split on \n and also used the list foreach... this resulted in a false comparison when it should not have in many instances.

I then removed the list command so it reads

Code: Select all

foreach s $cv_bannednicks {
that worked fine. So I'm concerned after reading: http://www.peterre.com/characters.html that my script may fail with some nicks. Using nicks like "[die]" could be real problematic without using list to escape the special characters.

I'm going to use the "element1" listing approach.
g
greenbear
Owner
Posts: 733
Joined: Mon Sep 24, 2001 8:00 pm
Location: Norway

Post by greenbear »

its not brainsurgery, store everything in lists (split), so you wont get any problems with special chars or [commands], then when doing matches or server outputs, convert it back to a string (join).
User avatar
De Kus
Revered One
Posts: 1361
Joined: Sun Dec 15, 2002 11:41 am
Location: Germany

Post by De Kus »

Solarin wrote:that worked fine. So I'm concerned after reading: http://www.peterre.com/characters.html that my script may fail with some nicks. Using nicks like "[die]" could be real problematic without using list to escape the special characters.
after 'set cv_bannednicks [split $cv_bannednicks \n]' a 'foreach s $cv_bannednicks' won't break on special characters, because split already did everything you have to worry about (the only 2 commands you would have worry would be "eval" and "expr", but you use neither of them :))
remember, in your usage $s and $nick are strings, so its best you compare them via string match.
De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens...
S
Solarin
Voice
Posts: 29
Joined: Wed Apr 09, 2003 1:50 pm
Location: Sydney Australia
Contact:

Post by Solarin »

Right.. well this may shock some of you then:

Code: Select all

set cv_bannednicks {[xxx]}
.
.
.
set cv_bannednicks [split $cv_bannednicks \n]
.
.
.
  set nick "[join [lindex [split $args " "] 6]]"

  foreach s $cv_bannednicks {
   if {[string match -nocase *$s* $nick]} { 
.
.
.
   }
 }
the nick SeXy_BeBuLs_16 was banned after I rehashed with the lastest version of the script.

Furthermore, I still haven't heard anything from anyone regarding my expr problem:

Code: Select all

set percent 0
set tcl_precision 2
set percent [expr $cv_klined / $cv_count * 100.00]
Why doesn't that return something (or why doesn't it return something other than 0)?
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

Solarin wrote:Furthermore, I still haven't heard anything from anyone regarding my expr problem:

Code: Select all

set percent 0
set tcl_precision 2
set percent [expr $cv_klined / $cv_count * 100.00]
Why doesn't that return something (or why doesn't it return something other than 0)?
what're $cv_klined and $cv_count ?
S
Solarin
Voice
Posts: 29
Joined: Wed Apr 09, 2003 1:50 pm
Location: Sydney Australia
Contact:

Post by Solarin »

They are incremented counters...

Code: Select all

set percent 0
set tcl_precision 2
set percent [expr $cv_banned / $cv_count * 100.00]
putserv "PRIVMSG $chan :Banned $cv_banned hosts out of $cv_count clients ($percent%).
That snippet is showing the following text in channel:
Banned 40 hosts out of 457 clients (0.0%).

Sorry... that was meant to be cv_banned instead of cv_klined for consistency sake.
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

try

Code: Select all

set percent [expr {double($cv_banned) / $cv_count * 100}]
or

Code: Select all

set percent [expr {($cv_banned) / ($cv_count + 0.0) * 100}]
check out expr.
S
Solarin
Voice
Posts: 29
Joined: Wed Apr 09, 2003 1:50 pm
Location: Sydney Australia
Contact:

Post by Solarin »

That worked great. Now if the bannednicks thing worked well with special nicks, I'd be set.
S
Solarin
Voice
Posts: 29
Joined: Wed Apr 09, 2003 1:50 pm
Location: Sydney Australia
Contact:

Post by Solarin »

Ok... this is really starting to bother me. Why don't my comparisons work correctly?

Code: Select all

set cv_bannedversions {
 "irc-ord"
 "http://www.ircap.com"
 "rbot"
 "jrbot"
 "http://iroffer.org/"
 "xen0"
 "sdbot"
 "xdcc catcher basic"
 "xdcc"
}
proc ctcp:version {nick uhost handle target key text} {
 set tempversionnumber 0
.
.
.
 foreach s $cv_bannedversions {
  if {[lsearch -glob [string tolower $text] "*$s*"] != -1} {
.
.
.
  }
  incr tempversionnumber
 }
}
with a version of XDCC Catcher Basic, $tempversionnumber is 8 not 7 (which is "xdcc" not "xdcc catcher basic"). This indicates that spaces in the element of $cv_bannedversions are making it choke up.

Furthermore, De Kus, the nick SeXy_BeBuLs_16 was banned with this code instead of nicks with [xxx] in them (so any nick with x in it will be banned):

Code: Select all

set cv_bannednicks {[xxx]}
.
.
.
set cv_bannednicks [split $cv_bannednicks \n]
.
.
.
  set nick "[join [lindex [split $args " "] 6]]"

  foreach s $cv_bannednicks {
   if {[string match -nocase *$s* $nick]} {
.
.
.
   }
 }
I haven't heard back in regards to this one yet.
User avatar
De Kus
Revered One
Posts: 1361
Joined: Sun Dec 15, 2002 11:41 am
Location: Germany

Post by De Kus »

are you sure you broke the foreach loop after the first match?
.loadtcl test
1: irc-ord
2: http://www.ircap.com
3: rbot
4: jrbot
5: http://iroffer.org/
6: xen0
7: sdbot
8: xdcc catcher basic
9: xdcc
the list is proccessed smoothly here using your code (with putdcc 7 "$tempversionnumber: $s" after the incr invoaction).

for the nick thing... strange. from the code you posted it doesnt make sense at all. *[xxx]* cant match SeXy_BeBuLs_16. you accidently used join on a string while setting the nick (lindex returns a string, lrange returns a list ^^), but i doubt that was the problem.
same goes with my intention of using split \n
.loadtcl test
firsttag
[tag2]
{tag3}
$tag4$
lasttag
De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens...
Locked