View previous topic :: View next topic |
Author |
Message |
romain Voice
Joined: 16 Oct 2005 Posts: 12
|
Posted: Wed Oct 19, 2005 9:20 am Post subject: array question |
|
|
hello,
I have this script
Code: | 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 |
|
Back to top |
|
 |
KrzychuG Master

Joined: 16 Aug 2003 Posts: 306 Location: Torun, Poland
|
Posted: Wed Oct 19, 2005 2:24 pm Post subject: |
|
|
Replace
Code: | [array get ::score] |
with
Code: | [lsort -decreasing [array get ::score]] |
_________________ Que? |
|
Back to top |
|
 |
De Kus Revered One

Joined: 15 Dec 2002 Posts: 1361 Location: Germany
|
Posted: Wed Oct 19, 2005 3:54 pm Post subject: |
|
|
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... |
|
Back to top |
|
 |
romain Voice
Joined: 16 Oct 2005 Posts: 12
|
Posted: Mon Oct 24, 2005 3:57 am Post subject: |
|
|
Hello,
I've more difficulty to write the results like this
Quote: | 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  |
|
Back to top |
|
 |
De Kus Revered One

Joined: 15 Dec 2002 Posts: 1361 Location: Germany
|
Posted: Mon Oct 24, 2005 7:08 am Post subject: |
|
|
try: Code: | 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 . 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 . _________________ 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... |
|
Back to top |
|
 |
romain Voice
Joined: 16 Oct 2005 Posts: 12
|
Posted: Mon Oct 24, 2005 12:13 pm Post subject: |
|
|
Yeah, it's ok.
Thanks |
|
Back to top |
|
 |
|