Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

All,
I have a question pertaining to traversing up/down lines once you've matched your $string.

You can find the scenario I'm dealing at this URL: http://soc.our.psu.edu/soc/fall/up/i-o/ist.html

Basically -- I wrote a script to track classes I need to register. It does the whole bit -- emails me when a slot opens, etc. I'm using the schedule number as my matching $string. So $string gets me to the line that holds all of the info for the particuliar class. The only info I cannot get from that line is the actual name of the class.

Example:
$string = "335794";
$_ = /$string/;
This would get me the line of the output which has 335794. Then from there I can do regex to parse out all of the data, such as remaining seats.
My question is how would I then traverse up x number of lines to get the class name, being "IST 230" in this scenario. The number x will not be a set number seeing as each class has a different number of sections available.

Background Info:
I'm using w3m -dump $url to get the output of the page. The -dump option just spits out what the page actually looks like -- it does NOT output the HTML source.

Thank you anyone who can help me out with this.

- Justin

Replies are listed 'Best First'.
Re: Regex :: Line Traversal
by BUU (Prior) on Jun 28, 2004 at 23:24 UTC
    Your best bet would probably be to try to parse each section in to some sort of datastructure, probably an array of hashes or something similar. Everytime you hit that "header section", start a new hash, then store the various class data. Then you can search through this array, find the section you want and have other data already there, ready to be used.
Re: Regex :: Line Traversal
by bsb (Priest) on Jun 29, 2004 at 09:23 UTC
    /(^IST.{6}).*(?:\n(?!^IST)|.)*(^335794.*)/m
    Find a line starting with IST, then either a \n not followed by IST or other characters, then your line number. Tweak the captures

    A solution based around a HTML/XML parser might be better. You may even be able to use an xpath query to get what you want

Re: Regex :: Line Traversal
by sweetblood (Prior) on Jun 28, 2004 at 23:13 UTC
    Try /\$string/

    $ Match the end of the line (or before newline at the end)

    perldoc perlre

    HTH

    Sweetblood