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.
Help for those learning Tcl or writing their own scripts.
bsdkid
Voice
Posts: 16 Joined: Wed Nov 02, 2005 1:04 am
Post
by bsdkid » Tue Dec 27, 2005 8:28 pm
Code: Select all
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?
demond
Revered One
Posts: 3073 Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:
Post
by demond » Wed Dec 28, 2005 12:51 am
Code: Select all
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
bsdkid
Voice
Posts: 16 Joined: Wed Nov 02, 2005 1:04 am
Post
by bsdkid » Wed Dec 28, 2005 9:20 pm
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
Sir_Fz
Revered One
Posts: 3794 Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:
Post
by Sir_Fz » Wed Dec 28, 2005 11:15 pm
I still don't understand your request.
demond
Revered One
Posts: 3073 Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:
Post
by demond » Thu Dec 29, 2005 12:03 am
oops I made a typo, I meant [lset] of course:
Code: Select all
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
bsdkid
Voice
Posts: 16 Joined: Wed Nov 02, 2005 1:04 am
Post
by bsdkid » Thu Dec 29, 2005 12:24 am
still get error demond
expected integer but got "bsdkid 9"
bsdkid
Voice
Posts: 16 Joined: Wed Nov 02, 2005 1:04 am
Post
by bsdkid » Thu Dec 29, 2005 12:28 am
Code: Select all
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.
demond
Revered One
Posts: 3073 Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:
Post
by demond » Thu Dec 29, 2005 1:05 am
[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: Select all
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
bsdkid
Voice
Posts: 16 Joined: Wed Nov 02, 2005 1:04 am
Post
by bsdkid » Thu Dec 29, 2005 7:38 am
You are awesome demond, thanks
bsdkid
Voice
Posts: 16 Joined: Wed Nov 02, 2005 1:04 am
Post
by bsdkid » Thu Dec 29, 2005 10:17 pm
demond, I have one more question, How do i reload the score array when the bot start?
demond
Revered One
Posts: 3073 Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:
Post
by demond » Fri Dec 30, 2005 1:11 am
Code: Select all
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
bsdkid
Voice
Posts: 16 Joined: Wed Nov 02, 2005 1:04 am
Post
by bsdkid » Fri Dec 30, 2005 7:21 am
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: Select all
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
demond
Revered One
Posts: 3073 Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:
Post
by demond » Fri Dec 30, 2005 11:49 pm
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
bsdkid
Voice
Posts: 16 Joined: Wed Nov 02, 2005 1:04 am
Post
by bsdkid » Sat Dec 31, 2005 12:03 am
how do i fix it demond?
demond
Revered One
Posts: 3073 Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:
Post
by demond » Sat Dec 31, 2005 12:09 am
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