I've sometimes thought how usefull it would be to have a small forum that would allow users to post procs etc (examples at the bottom) that could become usefull for others.
# Checks if command is a valid TCL command (includes procs)
proc istclcommand {command} {
if {[lsearch -exact [strlwr [info commands]] [strlwr $command]] != -1} {
return 1
}
return 0
}
# Gets line $lineno from a file
proc getline {file lineno} {
set i 0
set fp [open $file r]
while {![eof $fp]} {
set line [gets $fp]
incr i
if {$i==$lineno} {
close $fp
return $line
}
}
close $fp
return ""
}
(i'm sorry if this post doesnt seam usefull for you, but for me with quite much alcohol in my blood right now it does)
# Checks if command is a valid TCL command (includes procs)
proc istclcommand {command} {
if {[lsearch -exact [strlwr [info commands]] [strlwr $command]] != -1} {
return 1
}
return 0
}
This can be simplified (and adjusted for non-dependencies, as strlwr is an alltools.tcl proc).. Hrm... thought about it, and proc names/commands are case sensitive, so we definitley need case-sensitive comparisons.. so out with strlwr and string tolower altogether...
# Gets line $lineno from a file
proc getline {file lineno} {
set i 0
set fp [open $file r]
while {![eof $fp]} {
set line [gets $fp]
incr i
if {$i==$lineno} {
close $fp
return $line
}
}
close $fp
return ""
}
Reading line by line is alot slower than reading all the data at once...
# Gets line $lineno from a file
proc getline {file lineno} {
set infile [open $file r]
set buffer [read $infile]
close $infile
return [lindex [split $buffer \n] $lineno]
}
Using return at the end of the proc when the last comman executed returned what you want returned from the proc serves no purpose but slowing the execution down a bit. And how about a proc with glob matching chars in the name? (just being a grumpy old man tonight ;P)
strikelight wrote:
Reading line by line is alot slower than reading all the data at once...
Reading the entire file into memory without checking the size? I think a combination of your procs would be good...with a max size set some where
Using return at the end of the proc when the last comman executed returned what you want returned from the proc serves no purpose but slowing the execution down a bit. And how about a proc with glob matching chars in the name? (just being a grumpy old man tonight ;P)
In english, por favor?
Edit: I think i get what you mean... I'll give you that one..
Although, the 'return' is generally used for proper symantic rules.
strikelight wrote:
Reading line by line is alot slower than reading all the data at once...
user wrote:
Reading the entire file into memory without checking the size? I think a combination of your procs would be good...with a max size set some where
On todays computers, and the general text file, this isn't even an issue.
And even if it was, there is no combination that would make it more efficient.. The only thing that would add any sort of benefit, would be a max size check, if it were even an issue on today's computers.
Last edited by strikelight on Mon Aug 25, 2003 7:50 pm, edited 1 time in total.
user wrote:
Using return at the end of the proc when the last comman executed returned what you want returned from the proc serves no purpose but slowing the execution down a bit. And how about a proc with glob matching chars in the name? (just being a grumpy old man tonight ;P)
In english, por favor?
1) Procs return what ever was returned by the last command invoked inside them, so invoking "return" will give you the same result as just invoking expr, only a bit slower.
2) info commands accept glob patterns.
strikelight wrote:
On todays computers, and the general text file, this isn't even an issue.
And even if it was, there is no combination that would make it more efficient.. The only thing that would add any sort of benefit, would be a max size check, if it were even an issue on today's computers.
Some shell companies kill processes or even nuke shells that use too much memory/cpu. (I'm a victim of this :/)
user wrote:
1) Procs return what ever was returned by the last command invoked inside them, so invoking "return" will give you the same result as just invoking expr, only a bit slower.
Yes, already made an edit to my previous post regarding that..
user wrote:
2) info commands accept glob patterns.
They'll be matched
user wrote:
Some shell companies kill processes or even nuke shells that use too much memory/cpu. (I'm a victim of this :/)
Then you are doing more than just reading from a text file.
I'm probably missing some other special match characters, but you get the idea... (which is why I'm not _thrilled_ about this solution.. but hey, can't always have your cake and eat it too)
In the meantime if you want to post snippets you could just put them in the tcl forum and make the subject "Snippet: some good keywords" so they're easy to find. If there is later a snippets forum they could be moved.