parsing in wrong order?

Discussion in 'Perl Misc' started by Geoff Cox, Oct 12, 2004.

  1. Geoff Cox

    Geoff Cox Guest

    Hello,

    I have just come back to the kind of code shown in the extract below -
    problem is that although the code works, it does not output the data
    in the same order as in the html files.

    I would like to get successive sections of

    <h2>
    <p>
    <options>

    but am getitng all the <h2> and <p> and then all the <options> data.

    Can anyone see the "obvious" from the code below?

    The

    if ( $tagname eq 'option' ) {

    main::choice( $attr->{ value } );

    }

    ought, I would have thought, go off and get the <option> data and then
    return to work through the html in the order found in the html file??

    Cheers

    Geoff



    package MyParser;
    use base qw(HTML::parser);
    use strict;
    use diagnostics;

    my ($in_heading,$in_p, $fh);

    sub register_fh {
    $fh = $_[1];
    }

    sub reset { ($in_heading,$in_p)=(0,0)}

    sub start {

    my ( $self, $tagname, $attr, undef, $origtext ) = @_;

    if ( $tagname eq 'h2' ) {
    $in_heading = 1;
    return;
    }

    if ( $tagname eq 'p' ) {
    $in_p = 1;
    return;
    }

    if ( $tagname eq 'option' ) {

    main::choice( $attr->{ value } );

    }

    }

    sub end {
    my ( $self, $tagname, $origtext ) = @_;
    if ( $tagname eq 'h2' ) {
    $in_heading = 0;
    return;
    }

    if ( $tagname eq 'p' ) {
    $in_p = 0;
    return;
    }
    }

    sub text {
    my ( $self, $origtext ) = @_;
    print $fh "<h2>$origtext</h2> \n" if $in_heading;
    print $fh "<p>$origtext</p> \n" if $in_p;

    }

    package main;

    use File::Find;
     
    Geoff Cox, Oct 12, 2004
    #1
    1. Advertising

  2. Geoff Cox

    Geoff Cox Guest

    On Tue, 12 Oct 2004 20:20:13 GMT, Geoff Cox
    <> wrote:

    I seem to have found the error - of all things it seems to be an HTML
    error !

    Cheers

    Geoff

    >Hello,
    >
    >I have just come back to the kind of code shown in the extract below -
    >problem is that although the code works, it does not output the data
    >in the same order as in the html files.
    >
    > I would like to get successive sections of
    >
    ><h2>
    ><p>
    ><options>
    >
    >but am getitng all the <h2> and <p> and then all the <options> data.
    >
    >Can anyone see the "obvious" from the code below?
    >
    >The
    >
    > if ( $tagname eq 'option' ) {
    >
    > main::choice( $attr->{ value } );
    >
    > }
    >
    >ought, I would have thought, go off and get the <option> data and then
    >return to work through the html in the order found in the html file??
    >
    >Cheers
    >
    >Geoff
    >
    >
    >
    >package MyParser;
    >use base qw(HTML::parser);
    >use strict;
    >use diagnostics;
    >
    >my ($in_heading,$in_p, $fh);
    >
    >sub register_fh {
    >$fh = $_[1];
    >}
    >
    >sub reset { ($in_heading,$in_p)=(0,0)}
    >
    >sub start {
    >
    > my ( $self, $tagname, $attr, undef, $origtext ) = @_;
    >
    > if ( $tagname eq 'h2' ) {
    > $in_heading = 1;
    > return;
    > }
    >
    > if ( $tagname eq 'p' ) {
    > $in_p = 1;
    > return;
    > }
    >
    > if ( $tagname eq 'option' ) {
    >
    > main::choice( $attr->{ value } );
    >
    > }
    >
    >}
    >
    >sub end {
    > my ( $self, $tagname, $origtext ) = @_;
    > if ( $tagname eq 'h2' ) {
    > $in_heading = 0;
    > return;
    > }
    >
    > if ( $tagname eq 'p' ) {
    > $in_p = 0;
    > return;
    > }
    >}
    >
    >sub text {
    > my ( $self, $origtext ) = @_;
    > print $fh "<h2>$origtext</h2> \n" if $in_heading;
    > print $fh "<p>$origtext</p> \n" if $in_p;
    >
    >}
    >
    >package main;
    >
    >use File::Find;
     
    Geoff Cox, Oct 13, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Skating_vince
    Replies:
    2
    Views:
    414
    Skating_vince
    Aug 13, 2004
  2. Irishmaninusa

    Dropdown displaying data in the wrong order

    Irishmaninusa, Aug 26, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    448
    Irishmaninusa
    Aug 27, 2004
  3. Soren Kuula
    Replies:
    2
    Views:
    501
    Soren Kuula
    Feb 1, 2004
  4. cspoh
    Replies:
    0
    Views:
    250
    cspoh
    Jul 31, 2003
  5. Stephan Kämper
    Replies:
    2
    Views:
    237
    Stephan Kämper
    Jan 18, 2004
Loading...

Share This Page