Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re^4: Problem getting fields out of an XPath node list

by ejc1 (Novice)
on Mar 29, 2016 at 16:27 UTC ( #1159028=note: print w/replies, xml ) Need Help??

in reply to Re^3: Problem getting fields out of an XPath node list
in thread Problem getting fields out of an XPath node list

So, I don't understand why $cell->as_text gives the data, when Dumper \@cells prints a giant ton of garbage. Also, even though I have specified the table element as
my @tables = $tree->findnodes('//table'); my $table = $tables[12];
I cant reference this directly. Printing @cells[2]->as_text fails outright with "can't call method 'as_text' on an undefined value". It is clearly in there as
my @cells = $row->findnodes('td')
.... Anything I do to @cells flat out fails except for looking through with the mentioned
for my $cell (@cells)... print $cell->as_text
At this junction, I am about to totally give up on this, since I do not understand this at all and have no other way I can parse this otherwise. Since as_text dumps this one entry at a time, I was hoping to process the even elements of @cells as host/ip address and the odd as the previous elements port. But I just don't get this at all.

Replies are listed 'Best First'.
Re^5: Problem getting fields out of an XPath node list
by tangent (Parson) on Mar 29, 2016 at 17:19 UTC
    Note that the HTML block you provide is not valid (missing </td>, and a <tr> instead of </tr>). It would help if you showed us your desired output. It may be better to build up a Perl data structure first and then extract the values you need:
    # ... as before my @aoa; for my $row ( @rows ) { my @cells = $row->findnodes('td'); my @ary = map { $_->as_text } @cells; push( @aoa, \@ary ); } print Dumper( \@aoa ); print "Headers:\n"; my $headers = shift @aoa; print "$headers->[0], $headers->[1]\n"; print "Rows:\n"; for my $ary ( @aoa ) { print "$headers->[0]: $ary->[0], $headers->[1]: $ary->[1]\n"; } Output: $VAR1 = [ [ 'Remote Host', 'Port Number' ], [ '', '8091' ] ]; Headers: Remote Host, Port Number Rows: Remote Host:, Port Number: 8091

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1159028]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2023-09-28 13:31 GMT
Find Nodes?
    Voting Booth?

    No recent polls found