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.

string match question

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:

string match question

Post by NewzUK »

Hi - I'm working on a script to retrieve news headlines from a site - I use string match to identify the line in the html that has the info I want.

Normally when I do this, it gets the first line of the match, but in this case it's getting the last line (as there are many) at the bottom of the page...is there any way around this?

Thanks in advance.
#Newsroom - Where News & Markets Connect
http://www.inewsroom.net
#Newsroom on irc.othernet.org
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Re: string match question

Post by user »

I don't see how this is a string match problem. string match will not tell you where the match is, so the error must be in some other part of your code (obviously).
Have you ever read "The Manual"?
N
NewzUK
Master
Posts: 200
Joined: Mon Nov 18, 2002 3:10 am
Location: Auckland, New Zealand
Contact:

Post by NewzUK »

well I've used exactly the same script to get other pages and it's worked, anyway, here is the part I use to get the line:

Code: Select all

    foreach line [split $body \n] {
    if {[string match "*storylink*" $line]} {
and instead of getting the first line 'storylink' is in, it's getting the last. I'm not doing anything else to it except filtering the bits of the line I don't need.
#Newsroom - Where News & Markets Connect
http://www.inewsroom.net
#Newsroom on irc.othernet.org
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

What do you do inside that 'if'? You should probably 'break' out of the foreach loop :)
Have you ever read "The Manual"?
N
NewzUK
Master
Posts: 200
Joined: Mon Nov 18, 2002 3:10 am
Location: Auckland, New Zealand
Contact:

Post by NewzUK »

Code: Select all

 
   foreach line [split $body \n] {
   if {[string match "*color:blue;\"><b>*" $line]} {
    regexp {<b>(.*)</b>} $line - news
  }
 }
}
I tired putting a break line after the foreach, but then it does nothing :?
#Newsroom - Where News & Markets Connect
http://www.inewsroom.net
#Newsroom on irc.othernet.org
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

break exits the loop, you should add the break inside the if-statement since you already found your match in the first line. Of course, add the break after the needed statements inside the if-statement (in your case, after the regexp).
User avatar
rosc2112
Revered One
Posts: 1454
Joined: Sun Feb 19, 2006 8:36 pm
Location: Northeast Pennsylvania

Post by rosc2112 »

Wouldnt it be easier to just use regexp?

Code: Select all

if {[regexp {"*color:blue;"><b>(.*?)</b>"} $body fullmatch mymatch]} {
            # $mymatch has the data you're looking for
} else {
            #putcmdlog "no match"
}
Post Reply