One way to do this is to create a flag or a state variable to remember previous lines. For example, for the KEGG sections, I created a $kegg_found variable. Here is the revised version of the code:
#!/usr/bin/perl use strict; use warnings; # to modify cleangene # an infile (to be read in)and an outfile (to write to) # and both should be open my $infile = "clean.txt"; #output of batch entrez gene cleaned open (IN, $infile) or die "can't open file: $!"; my $outfile = "genetable.txt"; open (OUT, ">$outfile") or die "can't open file: $!"; my $kegg_found = 0; # reading one line at a time using the FILE handle while (<IN>) { if (/^\d+:\s\w.+/) { # disecting the first line into locus tag and + name my $name = $_; $name =~ s/\[\sArabidopsis\sthaliana\s\]|\[\sArabidopsis\s|\[/ +/; $name =~ s/^\d+:\s//; my @array = split /\s+/, $name; my $locus_tag = $array[0]; print OUT "$locus_tag\n \n"; $name =~ s/^AT\w+|\w+//; $name =~ s/^\s//; print OUT "$name\n"; } if (/^Function\sEvidence/) { print OUT "Unknown\n\n" unless $kegg_found; $kegg_found = 0; } next if /^Function\sEvidence|^Process\sEvidence|^Component\sEviden +ce|^\d+:\s\w.+/; if (/KEGG\spathway:|\w+\d{5}\s/){ #removing "KEGG pathway" from th +e kegg description my $kegg = $_; $kegg =~ s/^KEGG\spathway:\s//; $kegg =~ s/KEGG/ KEGG/g; $kegg =~ s/KEGG\spathway:\s//g; print OUT "$kegg"; $kegg_found = 1; } else { print OUT $_; } } close OUT; close IN;

This should solve your problem 3. You should be able to use this same principle to solve problem 4.

I also made a number of other changes to the code. I added the strictures:

use strict; use warnings;

This produced a warning (which I fixed):

Scalar value @array[0] better written as $array[0]

I declared all variables with my to eliminate the strict compiler errors.

I eliminated the capturing parentheses from all your regexes since you were not using the captured values.

I eliminated the unnecessary $_ =~

I closed the open file handles.

Hope this helps.


In reply to Re: parsing multiple lines by toolic
in thread parsing multiple lines by sm2004

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.