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

Hi...I need this help very badly...I have to do this program in Perl...Basically, the program has to read a HTML document and find a select tag, if it finds it then read the options for that tag and print it. Could anyone tell me how to go about it?

20040326 Edit by castaway: Changed title from 'perl program'

  • Comment on How to find SELECT tag in HTML and print its values?

Replies are listed 'Best First'.
Re: How to find SELECT tag in HTML and print its values?
by duff (Parson) on Mar 24, 2004 at 16:26 UTC
Re: How to find SELECT tag in HTML and print its values?
by Roy Johnson (Monsignor) on Mar 24, 2004 at 16:26 UTC
    I think step one would be to learn Perl well enough to take a shot at it yourself. That's probably one of the objectives of the class.

    The PerlMonk tr/// Advocate
      I know Perl...I am doing pattern matching to find option, but after that printing them is where I needed some help.
        Then try re-asking your question, telling us how far you've got, and what you can't figure out. For example, if you've found the lines that look like  <option value="whatever">, and you need to know how to parse out what's in the quotes, that would be your question.

        The PerlMonk tr/// Advocate
        I know Perl

        But does Perl know you? ;-)

        CountZero

        "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: How to find SELECT tag in HTML and print its values?
by kutsu (Priest) on Mar 24, 2004 at 16:27 UTC

    Some code would be nice, other then that look at CGI, LWP, WWW::Mechanize, and Ovid's Cgi course.

    "Cogito cogito ergo cogito sum - I think that I think, therefore I think that I am." Ambrose Bierce

      $file_name="index.htm"; open FILE, "$file_name" or die "can't read file: $!"; $char = getc FILE; while($char=<MYFILE>) { $pattern1="select class=form"; if(/$pattern1/i)#search for the string { $pattern2="option"; if(/$pattern2/i)#search for the options { $pattern3="value"; if(/$pattern3/i)#search if the options has value associa +ted with it { print $pattern4="/select"; open (MYFILE, ">save.txt") or die "no such file"; ............. .......... }//end first if statement }//end second if statement }//end while

      edit (broquaint): added <code> tags

        You've really got a lot of nonsense in your code. The getc, for example -- what's that about?

        It looks like you're expecting to find everything on the same line (select class, option, and value). If so, you might as well construct one pattern to extract what you want, rather than doing the nested ifs.

        For real HTML, using the modules that are designed to parse real HTML is the only sensible choice. If this is a learning exercise, you need to get a really clear idea of what you want your program to do, step-by-step, and then review your code to ensure that it is actually doing that.


        The PerlMonk tr/// Advocate
        Why do you read a single character from 'index.htm', which you then discard? Where's MYFILE coming from? Why are you opening the same handle you read from in your condition for writing in the body of the while?

        Abigail

        I know Perl

        You might want to brush up on the basics.