| View previous topic :: View next topic |
| Author |
Message |
Luminous Op
Joined: 12 Feb 2010 Posts: 146
|
Posted: Tue Jul 27, 2010 9:27 pm Post subject: problem matching indexes in file lines [solved] |
|
|
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 |
|
 |
willyw Revered One
Joined: 15 Jan 2009 Posts: 1175
|
Posted: Tue Jul 27, 2010 11:18 pm Post subject: |
|
|
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:
just before your while line.
I hope this helps.
reference:
http://www.tcl.tk/man/tcl8.5/TclCmd/seek.htm |
|
| Back to top |
|
 |
Luminous Op
Joined: 12 Feb 2010 Posts: 146
|
Posted: Tue Jul 27, 2010 11:44 pm Post subject: |
|
|
Wow, so simple... haha, usually is. Thanks man..  |
|
| Back to top |
|
 |
willyw Revered One
Joined: 15 Jan 2009 Posts: 1175
|
Posted: Tue Jul 27, 2010 11:45 pm Post subject: |
|
|
You are welcome. |
|
| Back to top |
|
 |
CrazyCat Revered One

Joined: 13 Jan 2002 Posts: 1032 Location: France
|
Posted: Thu Jul 29, 2010 3:08 am Post subject: |
|
|
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 |
|
 |
|