Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

HTML::TableExtract - Easy Way To Tell If It Found A Table?

by jdlev (Scribe)
on Sep 25, 2013 at 18:12 UTC ( #1055719=perlquestion: print w/replies, xml ) Need Help??
jdlev has asked for the wisdom of the Perl Monks concerning the following question:

I'm really struggling with trying to figure out if HTML::TableExtract is finding any legitimate tables. I'm trying to use the header method. Is there a simple way to print to screen whether the header method actually found a table, or if it doesn't find a table, have it print an error? Here's what I'm using:

use HTML::TableExtract; $te = HTML::TableExtract->new( headers => [qw(Date Price Cost)] ); $te->parse($html_string); # Examine all matching tables foreach $ts ($te->tables) { print "Table (", join(',', $ts->coords), "):\n"; foreach $row ($ts->rows) { print join(',', @$row), "\n"; } }

I love it when a program comes together - jdhannibal

Replies are listed 'Best First'.
Re: HTML::TableExtract - Easy Way To Tell If It Found A Table?
by hdb (Monsignor) on Sep 25, 2013 at 18:35 UTC

    Data::Dumper is your friend. Add use Data::Dumper; near the top of your script, and print Dumper $te; after $te->parse($html_string);.

Re: HTML::TableExtract - Easy Way To Tell If It Found A Table?
by kcott (Chancellor) on Sep 26, 2013 at 00:00 UTC

    G'day jdlev,

    "I'm really struggling with ..."

    I don't think pasting the first block of code that appears in the SYNOPSIS of the HTML::TableExtract documentation really involves any particular effort. It certainly doesn't equate to struggling!

    You're going to need to read and understand the documentation. Blindly copying code, and expecting it to perform whatever task you're thinking of, isn't going to work.

    The truth of

    scalar $te->tables

    will tell you whether any tables were found using the supplied headers criteria.

    You'll need to explain what you mean by "any legitimate tables". Possibly, the truth of

    scalar @{($te->tables)[0]->rows}

    is what you want.

    -- Ken

      So basically, I could just do an if statement like:

      if ($te->tables) { print "I found a table!"; }

      And something as simple as that would tell me if it found a table (as long as I called the parse function first)?

      I love it when a program comes together - jdhannibal
        "So basically, I could just do ..."

        Yes. Instead of typing the code into a forum, you could type it into your program.

        You could then run the program and find out what happens all by yourself.

        -- Ken

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1055719]
Approved by hdb
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2018-06-23 22:01 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.