Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Parsing block of texts

by Bod (Parson)
on Apr 02, 2021 at 21:35 UTC ( [id://11130744]=note: print w/replies, xml ) Need Help??


in reply to Parsing block of texts

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>

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-20 00:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found