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.

problem matching indexes in file lines [solved]

Help for those learning Tcl or writing their own scripts.
Post Reply
L
Luminous
Op
Posts: 146
Joined: Fri Feb 12, 2010 1:00 pm

problem matching indexes in file lines [solved]

Post by Luminous »

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.
w
willyw
Revered One
Posts: 1209
Joined: Thu Jan 15, 2009 12:55 am

Post by willyw »

Examine:

Code: Select all

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

seek $fs 0 start
just before your while line.

I hope this helps.

reference:
http://www.tcl.tk/man/tcl8.5/TclCmd/seek.htm
L
Luminous
Op
Posts: 146
Joined: Fri Feb 12, 2010 1:00 pm

Post by Luminous »

Wow, so simple... haha, usually is. Thanks man.. :)
w
willyw
Revered One
Posts: 1209
Joined: Thu Jan 15, 2009 12:55 am

Post by willyw »

:) You are welcome.
User avatar
CrazyCat
Revered One
Posts: 1334
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Post by CrazyCat »

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

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
Post Reply