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

Re: Parsing block of texts

by AnomalousMonk (Archbishop)
on Apr 03, 2021 at 01:12 UTC ( [id://11130753]=note: print w/replies, xml ) Need Help??


in reply to Parsing block of texts

I like the idea of processing the data (file) line-by-line, but I would parse the data records to a structure, an array of arrays of lines in which each top-level array element is a "chunk." Once you have the data parsed into a structure, you can do what you want with it.

Win8 Strawberry 5.8.9.5 (32) Fri 04/02/2021 20:38:05 C:\@Work\Perl\monks >perl use strict; use warnings; use autodie; use Data::Dump qw(dd); my $test_data = <<'EOD'; GO:0002366 False GO:0002446 True GO:0002275 True GO:0043312 True GO:0006733 False GO:0019674 False GO:0043588 False GO:0055065 False GO:0055080 True EOD use constant { 'TRUE' => 'True', 'FALSE' => 'False', }; my $rx_tf = qr{ \Q${ \TRUE }\E | \Q${ \FALSE }\E }xms; print "\$rx_tf $rx_tf \n"; # for debug open my $fh, '<', \$test_data; my @chunks; my $i_chunk = -1; while (my $record = <$fh>) { chomp $record; my $got_tf_rec = # true if record is valid my ($tf) = # extracted true/false field $record =~ m{ \A GO: \d{7} \s+ ($rx_tf) \Z }xms; die "bad t/f record: '$record'" if not $got_tf_rec; die "first t/f record not false: '$record'" if @chunks == 0 && $tf ne FALSE; ++$i_chunk if $tf eq FALSE; push @{ $chunks[ $i_chunk ] }, $record; } dd \@chunks; close $fh; exit; ^Z $rx_tf (?msx-i: True | False ) [ [ "GO:0002366 False", "GO:0002446 True", "GO:0002275 True", "GO:0043312 True", ], ["GO:0006733 False"], ["GO:0019674 False"], ["GO:0043588 False"], ["GO:0055065 False", "GO:0055080 True"], ]


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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-23 20:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found