in reply to Problems with TableExtract

Well... I am not familiar with either of these packages and it is odd, because the example of the table extract comes from the perldoc... but it doesn't return an object. By dumping the variables it did return I got it to work, however. Here is my code.
use WWW::Mechanize; use HTML::TableExtract; $url="http://www.dhl-usa.com/TransitTimes/USTTimeStart.asp?nav=Transit +Times"; $service = "Service"; $arrival = "Arrival Date and Time"; $transit = "Days in Transit*"; my $mech = WWW::Mechanize->new(); $mech->get($url); $frmSvcCalc = "frmSvcCalc"; $mech->form_name($frmSvcCalc); $mech->set_fields( txtOrgZip => "53213", txtDestZip => "60056", ); $mech->field( "hdnAction", "Calculate" ); $mech->submit($frmSvcCalc); my $results2 = $mech->content; $te = HTML::TableExtract->new( headers => ['Service','Arrival Date and + Time','Days in Transit*'] ); $te->parse($results2); foreach $ts ($te->tables) { foreach $row (@$ts) { print join(',', @$row), "\n"; } }
This, for me, prints each row you want on a line with each td separated by a comma, which should get you far enough along :)

Update I was using version 1.08 and looking at docs on search.cpan.org for 2.06, take with a grain of salt :)

                - Ant
                - Some of my best work - (1 2 3)

Replies are listed 'Best First'.
Re^2: Problems with TableExtract
by suaveant (Parson) on Jan 10, 2006 at 04:57 UTC
    Thinking about it, I should give you more info on what I did, so you can do it yourself in the future :)

    Personally I was just using print debugging. I started by getting

    Can't call method "coords" on unblessed reference at test.pl line 32.
    I printed out the value of $ts, which was an array, but was not blessed to a package. Since it had no package associated with it (when printing, HASH(0x864497c) is not blessed, HTML::TableExtract=HASH(0x864497c) is) that means you cannot call methods on it. So then I did print "@$ts\n"; and saw it was full of array refs. At this point you could loop through the array and print the results or do a print "@{$ts->[0]}\n";. Of course... the much better way to do this is use Data::Dumper and do a print Dumper($ts),"\n"; which would give you
    $VAR1 = [ [ 'DHL Next Day 10:30 am (Letter – 150 Pounds)', 'Tuesday, Jan 10, 2006 By 10:30 A.M.', '1' ], [ 'DHL Next Day 12:00 pm (Letter – 150 Pounds)', 'Tuesday, Jan 10, 2006 By Noon', '1' ], [ 'DHL Next Day 3:00 pm (Letter – 150 Pounds)', 'Tuesday, Jan 10, 2006 By 3:00 P.M.', '1' ], [ 'DHL 2nd Day Service (Letter – 150 Pounds)', 'Wednesday, Jan 11, 2006 By 5:00 P.M.', '2' ], [ 'DHL Ground Service (Letter – 150 Pounds)', 'Tuesday, Jan 10, 2006 By end of day', '1' ] ];
    Which shows the whole structure obviously.... quick and dirty debugging can often help you find out what is up, or you could always actually use the debugger, which I am usually too lazy to do :)

    Hopefully this helps even more than the first message.

    Any questions?

                    - Ant
                    - Some of my best work - (1 2 3)