in reply to parsing multiple lines

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.

Replies are listed 'Best First'.
Re^2: parsing multiple lines
by sm2004 (Acolyte) on May 22, 2008 at 00:49 UTC
    Thanks so much. Yes, it did help a lot. I can get it to work now.