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.

array question

Help for those learning Tcl or writing their own scripts.
Post Reply
r
romain
Voice
Posts: 12
Joined: Sun Oct 16, 2005 1:51 am

array question

Post by romain »

hello,

I have this script

Code: Select all

set ::kick(lolo) 10
set ::kick(lili) 15
set ::kick(kilo) 3
set ::kick(kili) 30

bind pub - !kick pub:test
proc pub:test {nick host hand chan arg} {
 foreach {x y} [array get ::kick] {
  putserv "privmsg $chan : $x with $y kicks"
 }
}
It works.But it's possible to write this list in order decreasing :?:
Last edited by romain on Mon Oct 24, 2005 3:58 am, edited 1 time in total.
User avatar
KrzychuG
Master
Posts: 306
Joined: Sat Aug 16, 2003 2:51 pm
Location: Torun, Poland
Contact:

Post by KrzychuG »

Replace

Code: Select all

[array get ::score]
with

Code: Select all

[lsort -decreasing [array get ::score]]
Que?
User avatar
De Kus
Revered One
Posts: 1361
Joined: Sun Dec 15, 2002 11:41 am
Location: Germany

Post by De Kus »

decreasing in which way? Value or element? For value (which would put the one with the most kicks to the top) you will need to add ' -integer -index 1' to the parameters of lsort.
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...
r
romain
Voice
Posts: 12
Joined: Sun Oct 16, 2005 1:51 am

Post by romain »

Hello,

I've more difficulty to write the results like this
1. kili with 30 kicks
2. lili with 15 kicks
3. lolo with 10 kicks
4. kilo with 3 kicks
if somebody can help me :?:
User avatar
De Kus
Revered One
Posts: 1361
Joined: Sun Dec 15, 2002 11:41 am
Location: Germany

Post by De Kus »

try:

Code: Select all

proc pub:test {nick host hand chan arg} {
  foreach {x y} [array get ::kick] {
    if {[string is integer -s $y]} {
      lappend score [list $x $y]
    }
  }
  set i 1
  foreach {e} [lsort -decreasing -integer -index 1 $score] {
    puthelp "PRIVMSG $chan :$i. [lindex $e 0] with [lindex $e 1] kicks"
    if {[incr i] == 10} {break}
  }
}
if you wonder why I used 2 foreach... the problem is that array get will not return a proper sublist, but just a list which uneven elements are the names and even ones are the values. So first a proper sublist must be created then we can use lsort to sort it :D. Change the '== 10' thing to whatever maximum number of results you want ;).
btw. the is integer check will prevent possible errors to occur in lsort from unproper array values. Since I dont know the script that created it, I wont trust it :D.
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...
r
romain
Voice
Posts: 12
Joined: Sun Oct 16, 2005 1:51 am

Post by romain »

Yeah, it's ok.
Thanks
Post Reply