egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How can i remove an empty line within a foreach

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
Elfriede
Halfop


Joined: 07 Aug 2007
Posts: 67

PostPosted: Tue Sep 14, 2010 8:31 am    Post subject: How can i remove an empty line within a foreach Reply with quote

Hi everboy Smile

Im having a small issue and appreciate any help Smile

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
View user's profile Send private message
willyw
Revered One


Joined: 15 Jan 2009
Posts: 1175

PostPosted: Tue Sep 14, 2010 10:57 am    Post subject: Re: How can i remove an empty line within a foreach Reply with quote

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
View user's profile Send private message
Luminous
Op


Joined: 12 Feb 2010
Posts: 146

PostPosted: Tue Sep 14, 2010 11:33 am    Post subject: Reply with quote

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. Smile
Back to top
View user's profile Send private message
speechles
Revered One


Joined: 26 Aug 2006
Posts: 1398
Location: emerald triangle, california (coastal redwoods)

PostPosted: Tue Sep 14, 2010 12:09 pm    Post subject: Reply with quote

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
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Wed Sep 15, 2010 3:41 pm    Post subject: Reply with quote

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
View user's profile Send private message
Elfriede
Halfop


Joined: 07 Aug 2007
Posts: 67

PostPosted: Fri Sep 17, 2010 2:41 am    Post subject: Reply with quote

Thanks @all for the great help Smile
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber