Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

splitting results

by bigup401 (Pilgrim)
on Dec 16, 2020 at 15:37 UTC ( [id://11125284]=perlquestion: print w/replies, xml ) Need Help??

bigup401 has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: splitting results
by GrandFather (Saint) on Dec 16, 2020 at 19:28 UTC

    You can't. You have no code. You have no data. You aren't using strictures and you aren't using an appropriate module from CPAN.

    Update: OP has updated splitting results several times since the node was first posted.

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
Re: splitting results
by NetWallah (Canon) on Dec 16, 2020 at 19:28 UTC
    You are unlikely to get a non-disparaging response, because your question is meaningless.

    It would be helpful to get what you want to transform, what you expect the result to be and what you have tried.

                    "Imaginary friends are a sign of a mental disorder if they cause distress, including antisocial behavior. Religion frequently meets that description"

Re: splitting results
by choroba (Cardinal) on Dec 16, 2020 at 23:08 UTC
    You didn't specify what the content was (it should be a table, right?) and how to get to the expected result from it. So, I imagined it.
    #!/usr/bin/perl use strict; use warnings; use HTML::TableExtract; my $html = "<table><tr><th>info\n<tr><td>john\n<tr><t" . "d>100 - 2000\n<tr><td>kent</table>"; my $table = 'HTML::TableExtract'->new(); $table->parse($html); print "Content-type: text/text\n\n"; my @rows = $table->rows; chomp @$_ for @rows; my $output = join ' ', 'data:' . $rows[0][0], "$rows[1][0]:" . do { $rows[2][0] =~ /- (...)/; + $1 }, "$rows[3][0]:" . substr $rows[2][0], 6, 4; print $output;

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: splitting results
by AnomalousMonk (Archbishop) on Dec 16, 2020 at 23:39 UTC

    Another guess, less elaborate than (but along similar lines as) choroba's here:

    Win8 Strawberry 5.8.9.5 (32) Wed 12/16/2020 18:30:38 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings my $s = <<EOT; info john 100 - 2000 kent EOT my @field = grep { not m{ \A - \z }xms } $s =~ m{ \S+ }xmsg ; $field[2] += 100; my $t = "data:$field[0] $field[1]:$field[2] $field[4]:$field[3]"; die "bad response '$t'" unless $t eq 'data:info john:200 kent:2000'; print "'$t' response ok \n"; ^Z 'data:info john:200 kent:2000' response ok


    Give a man a fish:  <%-{-{-{-<

Re: splitting results
by BillKSmith (Monsignor) on Dec 17, 2020 at 18:29 UTC
    The html you finally posted shows that you only have one row. It contains a lot of white space. You have parsed and dereferenced it correctly. All that is left is to clean up the white space and rearrange it.
    use strict; use warnings; use Data::Dumper; my $foo = 0; my $row = [ 'info', ' john ', ' 100 - 200 ', ' kent ', ]; print Dumper($row); foreach (@$row){ s/\A\s+//; s/\s+\z//; } print Dumper($row); my ($dat1, $dat2) = ${$row}[2] =~ m/(\d+)\D+(\d+)/; print "data:$row->[0] $row->[1]:$dat1 $row->[3]:$dat2", "\n";
    Bill
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: splitting results
by davido (Cardinal) on Dec 17, 2020 at 22:12 UTC

    I'm doing it in Mojo::DOM:

    #!/usr/bin/env perl use strict; use warnings; use Mojo::DOM; use Mojo::Util qw(trim); my $content = <<'HERE'; <td class="">info</td> <td> john </td> <td> 100 - 200 </td> <td> kent </td> HERE my $dom = Mojo::DOM->new($content); my ($data, $left_name, $range, $right_name) = map {trim($_->content)} +@{$dom->find('td')}; my ($left_val, $right_val) = split /\s*-\s*/, $range; print "data:$data $left_name:$left_val $right_name:$right_val\n";

    That produces:

    data:info john:100 kent:200


    Dave

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-16 13:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found