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.

How can i remove an empty line within a foreach

Help for those learning Tcl or writing their own scripts.
Post Reply
E
Elfriede
Halfop
Posts: 67
Joined: Tue Aug 07, 2007 4:21 am

How can i remove an empty line within a foreach

Post by Elfriede »

Hi everboy :)

Im having a small issue and appreciate any help :)

Code: Select all

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!
w
willyw
Revered One
Posts: 1209
Joined: Thu Jan 15, 2009 12:55 am

Re: How can i remove an empty line within a foreach

Post by willyw »

Code: Select all

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.
L
Luminous
Op
Posts: 146
Joined: Fri Feb 12, 2010 1:00 pm

Post by Luminous »

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: Select all

   foreach line [split $content \n] {
      if {![regexp {\s+} $line]} {
                   sendmsg $channel "$result $output"
      }
   }
} 
With llength:

Code: Select all

   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. :)
User avatar
speechles
Revered One
Posts: 1398
Joined: Sat Aug 26, 2006 10:19 pm
Location: emerald triangle, california (coastal redwoods)

Post by speechles »

Code: Select all

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"
      }
   }
} 
Last edited by speechles on Wed Sep 15, 2010 4:20 pm, edited 1 time in total.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

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
E
Elfriede
Halfop
Posts: 67
Joined: Tue Aug 07, 2007 4:21 am

Post by Elfriede »

Thanks @all for the great help :)
Post Reply