Code: Select all
foreach {word word} [regexp -all -inline {"http://www.url.com/e\?t=([^"]+)"} $html] {
lappend words $word
}
How can I write a comma between these words?WORD WORD2 WORD3 and so on...
Code: Select all
set text [join $yourlist ", "]
There's no single command to do thatJagg wrote:With which command I can say/split the output in two, three lines when var $a is more than 445 characters?
Code: Select all
proc wordwrap {str {len 70} {splitChr { }}} {
set out [set cur {}]; set i 0
foreach word [split [set str][unset str] $splitChr] {
if {[incr i [string len $word]]>$len} {
lappend out [join $cur $splitChr]
set cur [list $word]
set i [string len $word]
} {
lappend cur $word
}
incr i
}
lappend out [join $cur $splitChr]
}
What's the point of unsetting the local variable str? Sure you free up a little bit of memory, but with the command substitution you are adding cpu time... The local variable will be destroyed once the procedure has exited anyways, so for the little time it does spend inside the proc, plus the slight cpu overhead, there's no advantage to unsetting it.user wrote:There's no single command to do thatJagg wrote:With which command I can say/split the output in two, three lines when var $a is more than 445 characters?
This is a pretty straight forward word wrapper. the optional third value can be used to split on a different char than space (only one char..sorry ) It returns a list of "lines" that you can do whatever you like withps: it won't split words even if they're longer than the max length. feature or bug? you decideCode: Select all
proc wordwrap {str {len 70} {splitChr { }}} { set out [set cur {}]; set i 0 foreach word [split [set str][unset str] $splitChr] { if {[incr i [string len $word]]>$len} { lappend out [join $cur $splitChr] set cur [list $word] set i [string len $word] } { lappend cur $word } incr i } lappend out [join $cur $splitChr] }
pps: it will return a list with one empty element if you feed it an empty string
The point is to not have two copies of the entire input stored in memory at any point. In my opinion, saving ~50% memory (for large amounts of data) outweights the disadvantage of the tiny cpu usage added by the unset.strikelight wrote:What's the point of unsetting the local variable str? Sure you free up a little bit of memory, but with the command substitution you are adding cpu time... The local variable will be destroyed once the procedure has exited anyways, so for the little time it does spend inside the proc, plus the slight cpu overhead, there's no advantage to unsetting it.
Nothing wrong with us having a discussion about it thoughuser wrote:The point is to not have two copies of the entire input stored in memory at any point. In my opinion, saving ~50% memory (for large amounts of data) outweights the disadvantage of the tiny cpu usage added by the unset.strikelight wrote:What's the point of unsetting the local variable str? Sure you free up a little bit of memory, but with the command substitution you are adding cpu time... The local variable will be destroyed once the procedure has exited anyways, so for the little time it does spend inside the proc, plus the slight cpu overhead, there's no advantage to unsetting it.
But it probably doesn't matter for what these folks will use it for anyway...and to be perfectly honest I did it to make the boring code have a slightly interesting part for us to talk about right now
Code: Select all
% proc a {mylist} {set i 0; foreach word $mylist {incr i}}
% proc b {mylist} {set i 0; foreach word [set mylist][unset mylist] {incr i}}
% time {a $mylist} 1000
26 microseconds per iteration
% time {b $mylist} 1000
81 microseconds per iteration
% time {a $mylist} 1000
36 microseconds per iteration
% time {b $mylist} 1000
91 microseconds per iteration
Your test is not fair, as 'unset' returns an empty STRING, making the list before it become a string that has to be translated back to a list by foreach. (oh, the wonderfull internals of tcl ;P)strikelight wrote:Code: Select all
% proc a {mylist} {set i 0; foreach word $mylist {incr i}} % proc b {mylist} {set i 0; foreach word [set mylist][unset mylist] {incr i}
Code: Select all
proc a it {set i 0; foreach word [split $it] {incr i}; set i}
proc b it {set i 0; foreach word [split [set it][unset it]] {incr i}; set i}
You are correct...user wrote:Your test is not fair, as 'unset' returns an empty STRING, making the list before it become a string that has to be translated back to a list by foreachstrikelight wrote:Code: Select all
% proc a {mylist} {set i 0; foreach word $mylist {incr i}} % proc b {mylist} {set i 0; foreach word [set mylist][unset mylist] {incr i}
try this instead and you'll see the unset adds very little to the total cpu usageCode: Select all
proc a it {set i 0; foreach word [split $it] {incr i}; set i} proc b it {set i 0; foreach word [split [set it][unset it]] {incr i}; set i}
Code: Select all
% proc b {mylist} {set i 0; foreach word [split [set mylist][unset mylist]] {incr i}}
% proc a {mylist} {set i 0; foreach word [split $mylist] {incr i}}
% time {a $mylist} 1000
126 microseconds per iteration
% time {b $mylist} 1000
178 microseconds per iteration
It was 271 bytes....user wrote:How long is the string you're testing it with? To be on-topic it should be > 445 bytes
Oh..and what's the point of 'i'?
Code: Select all
% string bytelength $mylist
547
Code: Select all
% time {a $mylist} 1000
305 microseconds per iteration
% time {b $mylist} 1000
391 microseconds per iteration
% time {a $mylist} 1000
277 microseconds per iteration
% time {b $mylist} 1000
336 microseconds per iteration
Yeah...it seems most tcl versions are faster at setting the value to an empty string instead of deleting the entire variable, so I guess I should make itstrikelight wrote:heh. I guess we can agree, like most things, it's a trade off of memory vs performance.
Code: Select all
foreach word [split [set str][set str ""] $splitChr] {...}
From my tests, variable substitutions seem to be on exact par with command substitutions. (except for those command substitutions which also involve variable substitutions at the same time, of course).user wrote:Yeah...it seems most tcl versions are faster at setting the value to an empty string instead of deleting the entire variable, so I guess I should make itstrikelight wrote:heh. I guess we can agree, like most things, it's a trade off of memory vs performance.Code: Select all
foreach word [split [set str][set str ""] $splitChr] {...}
And to make it even faster I could replace all variable substitutions with [set ..], but then the code will be much harder to read.