| View previous topic :: View next topic |
| Author |
Message |
Elfriede Halfop
Joined: 07 Aug 2007 Posts: 67
|
Posted: Tue Sep 14, 2010 8:31 am Post subject: How can i remove an empty line within a foreach |
|
|
Hi everboy
Im having a small issue and appreciate any help
| Code: |
proc some:proc {nick host handle channel text} {
set url [lindex [split $text] 0]
set result [lindex $text 1]
set token [::http::geturl $url]
set content [::http::data $token]
::http::cleanup $token
foreach line [split $content \n] {
set output [lindex $line 0]
sendmsg $channel "$result $output"
}
}
|
If the lastline is an emptyline (its not always like this) i get $result without $output, as theres nothing to output. How can i drop that last/empty line ?
Thanks! |
|
| Back to top |
|
 |
willyw Revered One
Joined: 15 Jan 2009 Posts: 1175
|
Posted: Tue Sep 14, 2010 10:57 am Post subject: Re: How can i remove an empty line within a foreach |
|
|
| Code: |
proc some:proc {nick host handle channel text} {
set url [lindex [split $text] 0]
set result [lindex $text 1]
set token [::http::geturl $url]
set content [::http::data $token]
::http::cleanup $token
foreach line [split $content \n] {
set output [lindex $line 0]
if {"$output"!=""} {
sendmsg $channel "$result $output"
}
}
}
|
What happens when you try this?
I can't test it, so I'm curious if this simple change is all that is needed.
Even if this does work ok for you, there very well may be better ways to accomplish it. It will be interesting to see what else is posted here. |
|
| Back to top |
|
 |
Luminous Op
Joined: 12 Feb 2010 Posts: 146
|
Posted: Tue Sep 14, 2010 11:33 am Post subject: |
|
|
I'd want to test this myself to verify, but that method should be fine, as its checking to see if each line has something. Here's a few more ways I think would work, starting at that foreach loop:
With a regex:
| Code: |
foreach line [split $content \n] {
if {![regexp {\s+} $line]} {
sendmsg $channel "$result $output"
}
}
} |
With llength:
| Code: |
foreach line [split $content \n] {
if {[llength $line] != 0} {
sendmsg $channel "$result $output"
}
}
} |
Would you mind posting the output of your original as well as how to use it? Also, let us know how those work for you.  |
|
| Back to top |
|
 |
speechles Revered One

Joined: 26 Aug 2006 Posts: 1398 Location: emerald triangle, california (coastal redwoods)
|
Posted: Tue Sep 14, 2010 12:09 pm Post subject: |
|
|
| Code: | proc some:proc {nick host handle channel text} {
set stext [split $text]
set url [lindex $stext 0]
set result [lindex $stext 1]
set token [::http::geturl $url]
set content [::http::data $token]
::http::cleanup $token
foreach line [split $content \n] {
set output [string trim [lindex [split $line] 0]]
if {[string length $output]} {
sendmsg $channel "$result $output"
}
}
} |
_________________ speechles' eggdrop tcl archive
Last edited by speechles on Wed Sep 15, 2010 4:20 pm; edited 1 time in total |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Wed Sep 15, 2010 3:41 pm Post subject: |
|
|
A few comments on the posted codes:
Regular expressions are flexible, but far slower than other kinds of matching.
Using llength on a string is a bad idea. In worst case, llength might throw an error due to "improper list", which will immediately terminate the proc.
Same goes with using lindex on strings...
Both $text and $line are strings, not lists. Use the split-command to get a valid list, if you intend to use list operations. _________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
Elfriede Halfop
Joined: 07 Aug 2007 Posts: 67
|
Posted: Fri Sep 17, 2010 2:41 am Post subject: |
|
|
Thanks @all for the great help  |
|
| Back to top |
|
 |
|