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 

problem matching indexes in file lines [solved]

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
Luminous
Op


Joined: 12 Feb 2010
Posts: 146

PostPosted: Tue Jul 27, 2010 9:27 pm    Post subject: problem matching indexes in file lines [solved] Reply with quote

So, I have here, a mostly complete shortcut proc, so users can do like ~cs add word definition. To use it, they simply envoke ?word and get "definition". Problem is, I have no way to make sure the word they give doesn't already exist in the file. Every combination of while {[gets..., foreach, regex and string trim have failed.

There are two parts I need this kind of functionality: in the add { switch and then again in the list { switch section. They differ slightly, but I need a way to match only against the first element in $line.

Problem #2. For some reason, the while {[gets $fs line] >= 0} { line in add { switch does not loop through each line. I tried to make it notice me each line, but it would not. The one is list { is nearly identical, and it will at least notice me properly. But i also need that section to check index one of each line to see if it exists. if so, display 1 end.

http://paste.tclhelp.net/?id=7dr

Help is appreciated. :)


Last edited by Luminous on Fri Aug 13, 2010 1:35 pm; edited 2 times in total
Back to top
View user's profile Send private message
willyw
Revered One


Joined: 15 Jan 2009
Posts: 1175

PostPosted: Tue Jul 27, 2010 11:18 pm    Post subject: Reply with quote

Examine:
Code:

add {
            set fs [open "shortcuts.txt"]
            set data [split [read -nonewline $fs] \n]
            set check [join [lindex [split $text] 1]]
            while {[gets $fs line] >= 0} {

that section first.

The file is open, and I suspect the read command has left the file access at the very end of the file.
When you use gets , I think you mean to use it from the beginning of the file.

Thus, try inserting this line:
Code:

seek $fs 0 start

just before your while line.

I hope this helps.

reference:
http://www.tcl.tk/man/tcl8.5/TclCmd/seek.htm
Back to top
View user's profile Send private message
Luminous
Op


Joined: 12 Feb 2010
Posts: 146

PostPosted: Tue Jul 27, 2010 11:44 pm    Post subject: Reply with quote

Wow, so simple... haha, usually is. Thanks man.. Smile
Back to top
View user's profile Send private message
willyw
Revered One


Joined: 15 Jan 2009
Posts: 1175

PostPosted: Tue Jul 27, 2010 11:45 pm    Post subject: Reply with quote

Smile You are welcome.
Back to top
View user's profile Send private message
CrazyCat
Revered One


Joined: 13 Jan 2002
Posts: 1032
Location: France

PostPosted: Thu Jul 29, 2010 3:08 am    Post subject: Reply with quote

If the "database" is not too heavy, a good way is to read once the file and store it in an array, so you can easily answer to ?word without re-open and re-read the file.
And you can also immediatly check if the word already exists in the array.

Thats what I use in my jokes.tcl (only in reading) :
Code:
proc joke:load {} {
    set jp [open $::jname "r"]
    set jdata [read -nonewline $jp]
    close $jp
    set ::j_list ""
    foreach templine [split $jdata "\n"] {
        set joke_line [split $templine "="]
        set jkey [string tolower [lindex $joke_line 0]]
        if {[string index $jkey 0] == "!"} {
            set jkey [string range $jkey 1 end]
        } else {
            lappend ::j_list [string tolower $jkey]
        }
        set ::joke_key_gen($jkey) [lindex $joke_line 1]
        set ::joke_key($jkey) [lindex $joke_line 2]
        bind pub - ":$jkey*" joke:display
    }
}
joke:load

_________________
https://www.eggdrop.fr - French IRC network
Offer me a coffee - Do not ask me help in PM, we are a community.
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
Page 1 of 1

 
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