Rookie: HTML::TableExtract test will not print

Discussion in 'Perl Misc' started by sdfgsd, Oct 8, 2003.

  1. sdfgsd

    sdfgsd Guest

    Hello,

    This script will not generate any output within the foreach loop. No errors
    or anything. $te tests ok with a hash. I can't figure it out.

    My sample .html file consists of 1 table (depth = 0, count = 0). Using perl
    5.8/Suse 8.0

    Thanks in Advance
    ===========================
    #!/usr/bin/perl

    use warnings;
    use strict;
    use HTML::TableExtract;

    my $target = "op2.html";
    my $te;
    my $ts;
    my $row;

    if (-e $target) { print "File Exists\n"; }

    $te = new HTML::TableExtract( depth => 0, count => 0 );
    $te->parse($target);

    print "We are here\n";

    foreach $ts ($te->table_states) {
    print "Table found at ", join(',', $ts->coords), ":\n";
    foreach $row ($ts->rows) {
    print " ", join(',', @$row), "\n";
    }
    }
    ============================
     
    sdfgsd, Oct 8, 2003
    #1
    1. Advertising

  2. sdfgsd <> wrote:

    > This script will not generate any output within the foreach loop.



    Your code looks OK to me. Must be the data (that we weren't given).


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 9, 2003
    #2
    1. Advertising

  3. sdfgsd

    sdfgsd Guest

    > Your code looks OK to me. Must be the data (that we weren't given).

    Thanks for the follow-up. I'm hoping I can get this to work because this
    looks like a phenomonal tool...as opposed to having to write a new state
    machine/parser :)

    The .html file looks like this:
    =============================
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <html>
    <head>
    <title>Untitled</title>
    </head>

    <body>
    <table>
    <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    </tr>
    <tr>
    <td>4</td>
    <td>5</td>
    <td>6</td>
    </tr>
    <tr>
    <td>7</td>
    <td>8</td>
    <td>9</td>
    </tr>
    </table>

    </body>
    </html>
     
    sdfgsd, Oct 9, 2003
    #3
  4. sdfgsd

    Bob Walton Guest

    sdfgsd wrote:

    > Hello,
    >
    > This script will not generate any output within the foreach loop. No errors
    > or anything. $te tests ok with a hash. I can't figure it out.
    >
    > My sample .html file consists of 1 table (depth = 0, count = 0). Using perl
    > 5.8/Suse 8.0
    >
    > Thanks in Advance
    > ===========================
    > #!/usr/bin/perl
    >
    > use warnings;
    > use strict;
    > use HTML::TableExtract;
    >
    > my $target = "op2.html";


    ----------------^^^^^^^^

    The above string is the "HTML" you are supplying to the parse() method
    below. The parse method expects an HTML string, not a file name, so you
    need to read the file. Something like:

    my $target = "op2.html";
    open IN,$target or die "Oops, can't open $target for read, $!";
    {local $/;$target=<IN>} #slurp it
    close IN;


    > my $te;
    > my $ts;
    > my $row;
    >
    > if (-e $target) { print "File Exists\n"; }
    >
    > $te = new HTML::TableExtract( depth => 0, count => 0 );
    > $te->parse($target);
    >
    > print "We are here\n";
    >
    > foreach $ts ($te->table_states) {
    > print "Table found at ", join(',', $ts->coords), ":\n";
    > foreach $row ($ts->rows) {
    > print " ", join(',', @$row), "\n";
    > }
    > }

    ....


    HTH.
    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
     
    Bob Walton, Oct 9, 2003
    #4
  5. sdfgsd

    sdfgsd Guest

    (snipped)

    > > Thanks in Advance
    > > ===========================
    > > #!/usr/bin/perl
    > >
    > > use warnings;
    > > use strict;
    > > use HTML::TableExtract;
    > >
    > > my $target = "op2.html";

    >
    > ----------------^^^^^^^^
    >
    > The above string is the "HTML" you are supplying to the parse() method
    > below. The parse method expects an HTML string, not a file name, so you
    > need to read the file. Something like:
    >
    > my $target = "op2.html";
    > open IN,$target or die "Oops, can't open $target for read, $!";
    > {local $/;$target=<IN>} #slurp it
    > close IN;


    Thanks! This worked. If it's not too much trouble, could you give me a
    sentence or two on this construct:
    ==> {local $/;$target=<IN>}
    I know this'll send me scrambling to the books.


    > > my $te;
    > > my $ts;
    > > my $row;
    > >
    > > if (-e $target) { print "File Exists\n"; }
    > >
    > > $te = new HTML::TableExtract( depth => 0, count => 0 );
    > > $te->parse($target);
    > >
    > > print "We are here\n";
    > >
    > > foreach $ts ($te->table_states) {
    > > print "Table found at ", join(',', $ts->coords), ":\n";
    > > foreach $row ($ts->rows) {
    > > print " ", join(',', @$row), "\n";
    > > }
    > > }

    > ...
    >
    >
    > HTH.
    > --
    > Bob Walton
    > Email: http://bwalton.com/cgi-bin/emailbob.pl
    >
    >
     
    sdfgsd, Oct 9, 2003
    #5
  6. sdfgsd

    Sam Holden Guest

    On Thu, 09 Oct 2003 02:00:33 GMT, sdfgsd <> wrote:
    >
    > Thanks! This worked. If it's not too much trouble, could you give me a
    > sentence or two on this construct:
    >==> {local $/;$target=<IN>}
    > I know this'll send me scrambling to the books.


    Slurps the entire file into $target.

    See "perldoc perlvar" and the section on $/ for information on why.

    The block and local are used to limit the effect of the
    change to $/ (it is set to undef by the local) to just the
    read into $target.

    --
    Sam Holden
     
    Sam Holden, Oct 9, 2003
    #6
  7. sdfgsd

    sdfgsd Guest

    "Sam Holden" <> wrote in message
    news:...
    > On Thu, 09 Oct 2003 02:00:33 GMT, sdfgsd <> wrote:
    > >
    > > Thanks! This worked. If it's not too much trouble, could you give me a
    > > sentence or two on this construct:
    > >==> {local $/;$target=<IN>}
    > > I know this'll send me scrambling to the books.

    >
    > Slurps the entire file into $target.
    >
    > See "perldoc perlvar" and the section on $/ for information on why.
    >
    > The block and local are used to limit the effect of the
    > change to $/ (it is set to undef by the local) to just the
    > read into $target.


    Well put. This would have taken me a long time to internalize. Thank you.
     
    sdfgsd, Oct 9, 2003
    #7
    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. Paul

    Perl HTML::TableExtract Question

    Paul, Apr 17, 2005, in forum: Perl Misc
    Replies:
    3
    Views:
    222
  2. Jim Monty
    Replies:
    0
    Views:
    118
    Jim Monty
    May 16, 2005
  3. Maqo
    Replies:
    3
    Views:
    164
    Bob Walton
    May 25, 2005
  4. Abram

    HTML::TokeParser & TableExtract

    Abram, Apr 25, 2006, in forum: Perl Misc
    Replies:
    16
    Views:
    225
    David Combs
    May 22, 2006
  5. Ninja Li

    Parsing HTML with HTML::TableExtract

    Ninja Li, Nov 27, 2009, in forum: Perl Misc
    Replies:
    2
    Views:
    240
    Martien Verbruggen
    Nov 28, 2009
Loading...

Share This Page