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 with array

Help for those learning Tcl or writing their own scripts.
b
bsdkid
Voice
Posts: 16
Joined: Wed Nov 02, 2005 1:04 am

help with array

Post by bsdkid »

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?
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

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

Code: Select all

 tag when posting logs, code
b
bsdkid
Voice
Posts: 16
Joined: Wed Nov 02, 2005 1:04 am

Post by bsdkid »

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
User avatar
Sir_Fz
Revered One
Posts: 3793
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

I still don't understand your request.
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

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

Code: Select all

 tag when posting logs, code
b
bsdkid
Voice
Posts: 16
Joined: Wed Nov 02, 2005 1:04 am

Post by bsdkid »

still get error demond

expected integer but got "bsdkid 9"
b
bsdkid
Voice
Posts: 16
Joined: Wed Nov 02, 2005 1:04 am

Post by bsdkid »

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.
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

[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

Code: Select all

 tag when posting logs, code
b
bsdkid
Voice
Posts: 16
Joined: Wed Nov 02, 2005 1:04 am

Post by bsdkid »

You are awesome demond, thanks :)
b
bsdkid
Voice
Posts: 16
Joined: Wed Nov 02, 2005 1:04 am

Post by bsdkid »

demond, I have one more question, How do i reload the score array when the bot start?
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

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

Code: Select all

 tag when posting logs, code
b
bsdkid
Voice
Posts: 16
Joined: Wed Nov 02, 2005 1:04 am

Post by bsdkid »

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
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

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: Select all

 tag when posting logs, code
b
bsdkid
Voice
Posts: 16
Joined: Wed Nov 02, 2005 1:04 am

Post by bsdkid »

how do i fix it demond?
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

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: Select all

 tag when posting logs, code
Post Reply