Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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 (Vicar) 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' ], [ '192.5.5.241', '8091' ] ]; Headers: Remote Host, Port Number Rows: Remote Host: 192.5.5.241, Port Number: 8091

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2022-07-07 00:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?