| View previous topic :: View next topic |
| Author |
Message |
bsdkid Voice
Joined: 02 Nov 2005 Posts: 16
|
Posted: Tue Dec 27, 2005 8:28 pm Post subject: help with array |
|
|
| Code: |
if {[info exists score([string tolower $nick])]} {
incr score([string tolower $nick]) $pointvalue
} else {
set score([string tolower $nick]) $pointvalue
}
writescore $nick
|
Can you help me make it read BSDKID BSdKID and bsdKID be the same and keep the nick the way they are? |
|
| Back to top |
|
 |
demond Revered One

Joined: 12 Jun 2004 Posts: 3073 Location: San Francisco, CA
|
Posted: Wed Dec 28, 2005 12:51 am Post subject: |
|
|
| Code: |
set lnick [string tolower $nick]
if [info exists score($lnick)] {
set sco [lindex $score($lnick) 1]
set score($lnick) 1 [incr sco $points]
} {
set score($lnick) [list $nick $points]
}
|
i.e. you index by case-insensitive nick, but keep original nick in the array _________________ connection, sharing, dcc problems? click <here>
before asking for scripting help, read <this>
use [code] tag when posting logs, code |
|
| Back to top |
|
 |
bsdkid Voice
Joined: 02 Nov 2005 Posts: 16
|
Posted: Wed Dec 28, 2005 9:20 pm Post subject: |
|
|
demond, your script doesn't work good, it saves the score like this: bsdkid {bsdkid 6}, i just want bsdkid 6
and if second nick win i get this error.
[20:17] Tcl error [checkanswer]: expected integer but got "monk 6" and it swipped out the score array and write to file with null |
|
| Back to top |
|
 |
Sir_Fz Revered One

Joined: 27 Apr 2003 Posts: 3793 Location: Lebanon
|
|
| Back to top |
|
 |
demond Revered One

Joined: 12 Jun 2004 Posts: 3073 Location: San Francisco, CA
|
Posted: Thu Dec 29, 2005 12:03 am Post subject: |
|
|
oops I made a typo, I meant [lset] of course:
| Code: |
set lnick [string tolower $nick]
if [info exists score($lnick)] {
set sco [lindex $score($lnick) 1]
lset score($lnick) 1 [incr sco $points]
} {
set score($lnick) [list $nick $points]
}
|
and if you can't grasp what's being done here, you probably shouldn't aim to script; the idea is simple, the implementation even simpler - preserve the original nick, still indexing by lowercased nick (and since you can't have 2 values in array element - the original nick and the score - you need a list as array element) _________________ connection, sharing, dcc problems? click <here>
before asking for scripting help, read <this>
use [code] tag when posting logs, code |
|
| Back to top |
|
 |
bsdkid Voice
Joined: 02 Nov 2005 Posts: 16
|
Posted: Thu Dec 29, 2005 12:24 am Post subject: |
|
|
still get error demond
expected integer but got "bsdkid 9" |
|
| Back to top |
|
 |
bsdkid Voice
Joined: 02 Nov 2005 Posts: 16
|
Posted: Thu Dec 29, 2005 12:28 am Post subject: |
|
|
| Code: |
proc write_score {nick} {
global scorefile points score
if {![file exists $scorefile]} {
set fd [open $scorefile w]
close $fd
}
set lnick [string tolower $nick]
if [info exists score($lnick)] {
set sco [lindex $score($lnick) 1]
lset score($lnick) 1 [incr sco $points]
} {
set score($lnick) [list $nick $points]
}
|
Last edited by bsdkid on Thu Dec 29, 2005 7:38 am; edited 1 time in total |
|
| Back to top |
|
 |
demond Revered One

Joined: 12 Jun 2004 Posts: 3073 Location: San Francisco, CA
|
Posted: Thu Dec 29, 2005 1:05 am Post subject: |
|
|
[array get] returns a list of key-value pairs (not lists!); in your case, the value itself is a pair (list!) containing the original nick and its score; so you need to [lsort] that list of lists by the index of score:
| Code: |
foreach {k v} [array get score] {lappend pairs $v}
foreach pair [lsort -integer -decreasing -index 1 $pairs] {
foreach {n s} $pair {puts $f "$n $s"}
}
|
_________________ connection, sharing, dcc problems? click <here>
before asking for scripting help, read <this>
use [code] tag when posting logs, code |
|
| Back to top |
|
 |
bsdkid Voice
Joined: 02 Nov 2005 Posts: 16
|
Posted: Thu Dec 29, 2005 7:38 am Post subject: |
|
|
You are awesome demond, thanks  |
|
| Back to top |
|
 |
bsdkid Voice
Joined: 02 Nov 2005 Posts: 16
|
Posted: Thu Dec 29, 2005 10:17 pm Post subject: |
|
|
| demond, I have one more question, How do i reload the score array when the bot start? |
|
| Back to top |
|
 |
demond Revered One

Joined: 12 Jun 2004 Posts: 3073 Location: San Francisco, CA
|
Posted: Fri Dec 30, 2005 1:11 am Post subject: |
|
|
| Code: |
set f [open score.txt]
foreach line [split [read $f]] {
foreach {n s} [split $line] {
set score($n) [list $n $s]
}
}
close $f
|
_________________ connection, sharing, dcc problems? click <here>
before asking for scripting help, read <this>
use [code] tag when posting logs, code |
|
| Back to top |
|
 |
bsdkid Voice
Joined: 02 Nov 2005 Posts: 16
|
Posted: Fri Dec 30, 2005 7:21 am Post subject: |
|
|
demond, i got 1 small error when i next start the bot, after reload the score array it says
Currently: expected integer but got ""
Currently: while executing
Currently: "lsort -integer -decreasing -index 1 $pairs"
Currently: (procedure "first" line 102)
Currently: invoked from within
| Code: |
set f [open $scorefile w]
set pairs {}; foreach {n s} [array get score] {lappend pairs [list $n $s]}
foreach pairs [lsort -integer -decreasing -index 1 $pairs] {
puts $f $pairs
}
close $f
|
|
|
| Back to top |
|
 |
demond Revered One

Joined: 12 Jun 2004 Posts: 3073 Location: San Francisco, CA
|
Posted: Fri Dec 30, 2005 11:49 pm Post subject: |
|
|
then you must have messed up nicks (strings) with scores (integers) _________________ connection, sharing, dcc problems? click <here>
before asking for scripting help, read <this>
use [code] tag when posting logs, code |
|
| Back to top |
|
 |
bsdkid Voice
Joined: 02 Nov 2005 Posts: 16
|
Posted: Sat Dec 31, 2005 12:03 am Post subject: |
|
|
| how do i fix it demond? |
|
| Back to top |
|
 |
demond Revered One

Joined: 12 Jun 2004 Posts: 3073 Location: San Francisco, CA
|
Posted: Sat Dec 31, 2005 12:09 am Post subject: |
|
|
by understanding what exactly is being done by the code I gave you, and what exactly you've done with it then the bug will become obvious to you and you'll be able to fix it yourself _________________ connection, sharing, dcc problems? click <here>
before asking for scripting help, read <this>
use [code] tag when posting logs, code |
|
| Back to top |
|
 |
|