Rookie: HTML::TableExtract test will not print

S

sdfgsd

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";
}
}
============================
 
T

Tad McClellan

sdfgsd said:
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).
 
S

sdfgsd

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>
 
B

Bob Walton

sdfgsd said:
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.
 
S

sdfgsd

(snipped)
----------------^^^^^^^^

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.
 
S

Sam Holden

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.
 
S

sdfgsd

Sam Holden said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top