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

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

by tangent (Vicar)
on Mar 29, 2016 at 15:51 UTC ( #1159025=note: print w/replies, xml ) Need Help??


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

Finding the value of the list element is not really helping you as the table is not an element of the list. If you know there is only one table, this verbose example may help:
# get all the tables my @tables = $tree->findnodes('//table'); # get the first table my $table = $tables[0]; # get all the rows of first table my @rows = $table->findnodes('tr'); # loop through the rows for my $row ( @rows ) { # get all the cells my @cells = $row->findnodes('td'); # loop through the cells for my $cell ( @cells ) { print $cell->as_text, "\n"; } } Output: Remote Host Port Number 192.5.5.241 8091

Replies are listed 'Best First'.
Re^4: Problem getting fields out of an XPath node list
by ejc1 (Novice) on Mar 29, 2016 at 16:27 UTC
    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.
      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://1159025]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (1)
As of 2022-05-22 02:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (78 votes). Check out past polls.

    Notices?