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.

What about a 'Snippets' forum ?

Old posts that have not been replied to for several years.
Locked
User avatar
GodOfSuicide
Master
Posts: 463
Joined: Mon Jun 17, 2002 8:00 pm
Location: Austria

What about a 'Snippets' forum ?

Post by GodOfSuicide »

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.

Code: Select all

# 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)
User avatar
strikelight
Owner
Posts: 708
Joined: Mon Oct 07, 2002 10:39 am
Contact:

Re: What about a 'Snippets' forum ?

Post by strikelight »

GodOfSuicide wrote:

Code: Select all

# 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...

Code: Select all

proc istclcommand {command} { return [expr {[info commands $command] != ""}] }
GodOfSuicide wrote:

Code: Select all

# 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...

Code: Select all

# 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]
}
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Re: What about a 'Snippets' forum ?

Post by user »

strikelight wrote:

Code: Select all

proc istclcommand {command} { return [expr {[info commands $command] != ""}] }
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 :P
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Here's my snippet...the amazing LIST proc:

Code: Select all

proc LIST args {set args}
User avatar
strikelight
Owner
Posts: 708
Joined: Mon Oct 07, 2002 10:39 am
Contact:

Re: What about a 'Snippets' forum ?

Post by strikelight »

user wrote:
strikelight wrote:

Code: Select all

proc istclcommand {command} { return [expr {[info commands $command] != ""}] }
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..

Code: Select all

proc istclcommand {command} { expr {[info commands $command] != ""} }
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 :P
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 avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Re: What about a 'Snippets' forum ?

Post by user »

strikelight wrote:
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 avatar
strikelight
Owner
Posts: 708
Joined: Mon Oct 07, 2002 10:39 am
Contact:

Re: What about a 'Snippets' forum ?

Post by strikelight »

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 :wink:

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.
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Re: What about a 'Snippets' forum ?

Post by user »

strikelight wrote:They'll be matched :wink:
And then some :lol: (risking false positives)
strikelight wrote:Then you are doing more than just reading from a text file.
I was sorting a ~60Mb dictionary (text file) in memory :cry:
User avatar
strikelight
Owner
Posts: 708
Joined: Mon Oct 07, 2002 10:39 am
Contact:

Re: What about a 'Snippets' forum ?

Post by strikelight »

user wrote:
strikelight wrote:They'll be matched :wink:
And then some :lol: (risking false positives)
Yea .. I know.. still mulling over a simpler solution than a lsearch... I'll get back to ya ;>
user wrote:
strikelight wrote:Then you are doing more than just reading from a text file.
I was sorting a ~60Mb dictionary (text file) in memory :cry:
Ya.. reading a file that large could cause a bit of a freeze up.. but why do you have a 60mb quota =p

But what I suspect really was the culprit, was the non-optimized sorting implementation, but that's just pure speculation on my part.
User avatar
strikelight
Owner
Posts: 708
Joined: Mon Oct 07, 2002 10:39 am
Contact:

Re: What about a 'Snippets' forum ?

Post by strikelight »

strikelight wrote:
Yea .. I know.. still mulling over a simpler solution than a lsearch... I'll get back to ya ;>
Not too happy with this, but, I think it's more efficient anyways:

Code: Select all

proc istclcommand {command} { expr {[info commands [string map {"*" "\\*" "?" "\\?"} $command]] != ""} } 
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)
User avatar
GodOfSuicide
Master
Posts: 463
Joined: Mon Jun 17, 2002 8:00 pm
Location: Austria

Post by GodOfSuicide »

the main question was what you think about such a forum, not about the crappy post i made :D
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

GodOfSuicide wrote:the main question was what you think about such a forum, not about the crappy post i made :D
I think such a forum would be great. (it would lead to lots of lame discussions like the one you've seen here though ;P)
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

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.
User avatar
GodOfSuicide
Master
Posts: 463
Joined: Mon Jun 17, 2002 8:00 pm
Location: Austria

Post by GodOfSuicide »

it could be added quite easy : remove the TCL Faq board (was there ever anyone using it ?) and add this one :D
Locked