what is the "%" and the space for?bind pubm -|- "% !p*" que:poem
% matches 0 or more non-space characters (can be used to match a single word) ...and in a pubm bind the mask is matched against the channel name followed by the text, so the % is used to match any channel name. (you could have used *, but then you'd risk matching things that doesn't start with the trigger)Dedan wrote:what is the "%" and the space for?
Code: Select all
set line_wanted 75
if {[file exists $my_file]} {
set opened_file [open $my_file r]
need to check if line exsist
need to retrive line
set found_line ??????
Code: Select all
proc getLineIfItExists {file {line 0}} {
set lines [split [read [set file [open $file]]] \n]
close $file
if {[llength $lines]>$line} {
lindex $lines $line
} else {
error "No such line: $line"
}
}
Code: Select all
set line_wanted 75
if {[file exists $my_file]} {
set opened_file [open $my_file r]
if {[info exists [seek opened_file 75 start]]} {
set found_line [seek opened_file 75 start]
}
}
'info exists' is for checking for the existance of variables. 'seek' is for changing the position in a open file and works with bytes, not lines, so unless your file contains only newlines this doesn't make any sense.Dedan wrote:do you think this might work?
I can see why you're intimidated by this as you're used to mirc's file commands. Tcl deals with files like real programming languages; you get full control at the cost of a bit more coding work. There are no lines in a file, only bytes. :^)Dedan wrote:I am overwhelmed by the commands of tcl to deal with files, are you saying that there is no way to read a certain line
without knowing its lenght?
Let me repeat with commentsDedan wrote:In your last proc you showed no way to retrevie the line
so it could be set.
Code: Select all
proc getLineIfItExists {file {line 0}} {
# this first line does alot of stuff...
# first the file is opened for reading (the default mode, so the "r" is not needed
# then the file pointer returned is stored in the variable "file"
# set returns the value further up to 'read' which reads the entire file
# and returns the contents to 'split' which chops it up by the newlines (\n)
# the resulting list of lines is then stored in 'lines'
set lines [split [read [set file [open $file]]] \n]
# then the file is closed (as we don't need to read from it no more)
close $file
# and THEN we check if the number of lines is > our desired line #
if {[llength $lines]>$line} {
# if it is, we retrieve the line we want using lindex (this value is then returned by the proc...you could of course store it in a variable or what ever you might desire
lindex $lines $line
} else {
# if the requested line number is too high, generate an error.
error "No such line: $line"
}
}
If the contents is fairly static you could compile some sort of index...eg. a list of line lengths (this way you could skip directly to the desired line using 'seek')Dedan wrote:this file has about 2500 lines, is there anyway
to get aroung reading all those lines?
Code: Select all
proc getLine {file {line 1}} {
set f [open $file]
set i 0
while {[incr i]<$line} {gets $f}
gets $f line
close $f
set line
}
Code: Select all
set line ""
set line_wanted 75
if {[file exists $my_file]} {
set opened_file [open $my_file r]
set line_number 0
while {![eof $opened_file]} {
incr line_number 1
if {$line_number == $line_wanted} {
set line [gets $opened_file]
break
}
}
if {$line == ""} {
*** error message ***
}
like i said before, get tclsh running on your local machineDedan wrote:like i said before, my shells are down
i have no way of testing this.
This is how my previous proc works...'gets' reads every byte from the current position in the file to the next newline (or the end of the file, whichever comes first). So each call to 'gets' will cause the access position in the file channel to be moved one line further towards the end of the file.Dedan wrote:do you think this would work?
...