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

I'm trying to parse a block of text of the following structure:
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
I want to get chunks of the following texts( "True" cases under each "False" cases are part of the "False" cases Otherwise the False case has no True cases under it)
#Chunk 0: GO:0002366 False GO:0002446 True GO:0002275 True GO:0043312 True #Chunk 1: GO:0006733 False #Chunk 2: GO:0019674 False #Chunk 3: GO:0043588 False #Chunk 4: GO:0055065 False GO:0055080 True
Can somebody help me doing this ? I'm trying to split the input file read by slurp mode, but not sure what /$pattern/, I would use: my @chunks = split(/($pattern)/, $data);

Replies are listed 'Best First'.
Re: Parsing block of texts
by kcott (Archbishop) on Apr 02, 2021 at 21:51 UTC

    G'day perlance,

    Reading the entire file contents into a string, and then reading all of that data again to parse it, is not very efficent: instead, parse the data as you're reading it (once). I also think you're completely on the wrong track with '... split(/($pattern)/ ...'; to be honest, I'm not really sure what you were hoping to achieve with that.

    All you probably need is something simple like this:

    #!/usr/bin/env perl use strict; use warnings; my $chunk = -1; while (<DATA>) { if ((split)[1] eq 'False') { ++$chunk; print "#Chunk $chunk:\n"; } print; } __DATA__ 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

    Output:

    #Chunk 0: GO:0002366 False GO:0002446 True GO:0002275 True GO:0043312 True #Chunk 1: GO:0006733 False #Chunk 2: GO:0019674 False #Chunk 3: GO:0043588 False #Chunk 4: GO:0055065 False GO:0055080 True

    — Ken

Re: Parsing block of texts
by tybalt89 (Monsignor) on Apr 02, 2021 at 21:26 UTC
    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11130738 use warnings; local $_ = <<END; 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 END while( /^(.*False\n(?:.*True\n)*)/gm ) { print "$1\n"; }

    Outputs:

    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
Re: Parsing block of texts
by AnomalousMonk (Archbishop) on Apr 03, 2021 at 01:12 UTC

    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:  <%-{-{-{-<

Re: Parsing block of texts
by Bod (Parson) on Apr 02, 2021 at 21:35 UTC

    I would use a loop to parse the input...

    use strict; use warnings; my $chunk = 0; my $flag = 0; open my $fh, '<', "data.txt"; while (my $line = <$fh>) { chomp $line; my ($code, $state) = split / +/, $line; if ($flag and $state eq 'False') { print "\n#Chunk $chunk\n"; $chunk++; } print "$line\n"; $flag = 1; }
    The output is:
    C:\Users\user\Perl>perl test.pl GO:0002366 False GO:0002446 True GO:0002275 True GO:0043312 True #Chunk 0 GO:0006733 False #Chunk 1 GO:0019674 False #Chunk 2 GO:0043588 False #Chunk 3 GO:0055065 False GO:0055080 True

    UPDATE: I've just noticed you want #Chunk 0 at the top so no $flag is needed to suppress it...

    use strict; use warnings; my $chunk = 0; open my $fh, '<', "data.txt"; while (my $line = <$fh>) { chomp $line; my ($code, $state) = split / +/, $line; if ($state eq 'False') { print "\n#Chunk $chunk\n"; $chunk++; } print "$line\n"; }

    Output:

    C:\Users\user\Perl>perl test.pl #Chunk 0 GO:0002366 False GO:0002446 True GO:0002275 True GO:0043312 True #Chunk 1 GO:0006733 False #Chunk 2 GO:0019674 False #Chunk 3 GO:0043588 False #Chunk 4 GO:0055065 False GO:0055080 True C:\Users\user\Perl>