On 07/12/2010 20:42, Jim Gibson wrote:
On 12/7/10 Tue  Dec 7, 2010  12:17 PM, "shawn wilson"<ag4ve...@gmail.com>
scribbled:

i'm messing up somewhere along the way here...

i'm trying to get data from a table in a page which should always get
defined like this:

<table class="schiffsdetails" style="width:508px;" id="Any_20">
<tbody>

and i'm looking for an element of the table that looks like this:

<td>Length:</td>
<td>266.0m</td>


i'm fine getting the url but i'm getting this four times when i try to run
the script:
Use of uninitialized value in string eq at ./vt-getlen.pl line 36,<FILE>
line 2.

and the tokeparser looks like this:
    my $parser = HTML::TokeParser::Simple->new( string =>  $content )
       or die "Can't define content to parser $!";

    while( my $table = $parser->get_tag( 'table' ) ) {
       next unless $table->[ 1 ]{ 'class' } eq "schiffsdetails";
       while( $parser->get_tag( "td" ) ) {
          next unless $parser->get_text( "/td" ) eq "Length";
          $parser->get_tag( "td" );
          my $length = $parser->get_text( "/td" );
          print "$data[ 0 ], $data[ 1 ], $length\n";
       }
    }

i'm calling tokeparser on line 32. any ideas?

According to the HTML::TokeParser::Simple documentation, get_tag() returns
the tag returned by HTML::TokeParser. According to the HTML::TokeParser
documentation, get_tag() returns an array reference giving [ $tag, $attr,
$attrseq, $text ]. So it is possible that if there are no attributes in a
table tag entry, $attr will be undefined.

If that is the case, then you should be testing $table->[1] for being
defined before using it in an expression. Try printing the value of $table
to see what it contains. See the Data::Dumper module for a way to do that.

If an element has no attributes then TokeParser will return an empty
hash in $table->[1], rather than undef. However, if a <table> element is
picked up that has no 'class' attribute, then $table->[1]{class} will be
undefined - hence the warning messages.

The fix is to write

  my $class = $table[1]{class};
  next unless $class and $class eq 'schiffsdetails';

HTH,

Rob

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to