egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

help with array
Goto page 1, 2  Next
 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
bsdkid
Voice


Joined: 02 Nov 2005
Posts: 16

PostPosted: Tue Dec 27, 2005 8:28 pm    Post subject: help with array Reply with quote

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
View user's profile Send private message
demond
Revered One


Joined: 12 Jun 2004
Posts: 3073
Location: San Francisco, CA

PostPosted: Wed Dec 28, 2005 12:51 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
bsdkid
Voice


Joined: 02 Nov 2005
Posts: 16

PostPosted: Wed Dec 28, 2005 9:20 pm    Post subject: Reply with quote

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
View user's profile Send private message
Sir_Fz
Revered One


Joined: 27 Apr 2003
Posts: 3793
Location: Lebanon

PostPosted: Wed Dec 28, 2005 11:15 pm    Post subject: Reply with quote

I still don't understand your request.
_________________
Follow me on GitHub

- Opposing

Public Tcl scripts
Back to top
View user's profile Send private message Visit poster's website
demond
Revered One


Joined: 12 Jun 2004
Posts: 3073
Location: San Francisco, CA

PostPosted: Thu Dec 29, 2005 12:03 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
bsdkid
Voice


Joined: 02 Nov 2005
Posts: 16

PostPosted: Thu Dec 29, 2005 12:24 am    Post subject: Reply with quote

still get error demond

expected integer but got "bsdkid 9"
Back to top
View user's profile Send private message
bsdkid
Voice


Joined: 02 Nov 2005
Posts: 16

PostPosted: Thu Dec 29, 2005 12:28 am    Post subject: Reply with quote

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
View user's profile Send private message
demond
Revered One


Joined: 12 Jun 2004
Posts: 3073
Location: San Francisco, CA

PostPosted: Thu Dec 29, 2005 1:05 am    Post subject: Reply with quote

[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
View user's profile Send private message Visit poster's website
bsdkid
Voice


Joined: 02 Nov 2005
Posts: 16

PostPosted: Thu Dec 29, 2005 7:38 am    Post subject: Reply with quote

You are awesome demond, thanks Smile
Back to top
View user's profile Send private message
bsdkid
Voice


Joined: 02 Nov 2005
Posts: 16

PostPosted: Thu Dec 29, 2005 10:17 pm    Post subject: Reply with quote

demond, I have one more question, How do i reload the score array when the bot start?
Back to top
View user's profile Send private message
demond
Revered One


Joined: 12 Jun 2004
Posts: 3073
Location: San Francisco, CA

PostPosted: Fri Dec 30, 2005 1:11 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
bsdkid
Voice


Joined: 02 Nov 2005
Posts: 16

PostPosted: Fri Dec 30, 2005 7:21 am    Post subject: Reply with quote

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
View user's profile Send private message
demond
Revered One


Joined: 12 Jun 2004
Posts: 3073
Location: San Francisco, CA

PostPosted: Fri Dec 30, 2005 11:49 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
bsdkid
Voice


Joined: 02 Nov 2005
Posts: 16

PostPosted: Sat Dec 31, 2005 12:03 am    Post subject: Reply with quote

how do i fix it demond?
Back to top
View user's profile Send private message
demond
Revered One


Joined: 12 Jun 2004
Posts: 3073
Location: San Francisco, CA

PostPosted: Sat Dec 31, 2005 12:09 am    Post subject: Reply with quote

by understanding what exactly is being done by the code I gave you, and what exactly you've done with it Smile 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
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber