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.

web query

Help for those learning Tcl or writing their own scripts.
Post Reply
N
NewzUK
Master
Posts: 200
Joined: Mon Nov 18, 2002 3:10 am
Location: Auckland, New Zealand
Contact:

web query

Post by NewzUK »

Hi - I`m trying to retrieve the first line of a list of headlines from a webpage, but instead it gets the last one at the bottom of the list...?

Here`s the first part of the script - 2007- is what I`m using to identify the lines...

set query "http://a-website-goes-here.com"
set token [http::geturl $query]
set all [http::data $token]
foreach line [split $all \n] {
if {[string match "*2007-*" $line]} {

Thanks in advance for any help
#Newsroom - Where News & Markets Connect
http://www.inewsroom.net
#Newsroom on irc.othernet.org
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

It gets the last line because of the foreach, each time it matches, it clobbers the 1st match.. So, you should take out the foreach, or alternatively use lappend to append each subsequent match and then use lindex 0 to get the 1st match.
N
NewzUK
Master
Posts: 200
Joined: Mon Nov 18, 2002 3:10 am
Location: Auckland, New Zealand
Contact:

Post by NewzUK »

great thanks for the help ros...it`s working fine now.

thanks again!
#Newsroom - Where News & Markets Connect
http://www.inewsroom.net
#Newsroom on irc.othernet.org
r
r0t3n
Owner
Posts: 507
Joined: Tue May 31, 2005 6:56 pm
Location: UK

Post by r0t3n »

or you could use break, instead of appending each result?

Code: Select all

set match ""
foreach line [split $all \n] {
  if {[string match *2007-* $line]} {
    set match "$line"
    break
  }
}
if {$match == ""} { return }
# continue with script
r0t3n @ #r0t3n @ Quakenet
Post Reply