in reply to Read, match string and print

Your first mistake is assigning values to $cds, $version and $gi and then using the test defined $cds and defined $gi and defined $version on values that are always defined.    And then your next mistake is not clearing out the values in the variables after you have printed them.

What you need is something more like this:

#!/usr/bin/perl use strict; use warnings; my $data_file = '/DATA/GenBankFile.gb'; open INFILE, '<', $data_file or die "Could not open '$data_file' $!"; my ( $cds, $gi, $version ); while ( <INFILE> ) { last if m!//$!; if ( /^VERSION.*\w:(\d+)/ ) { $version = $1; } elsif ( /^\s*\Sdb_xref="GI:(\d+)/ ) { $gi = $1; } elsif ( /^\s*CDS\s*(\S+)/ ) { $cds = $1; } if ( defined $cds && defined $gi && defined $version ) { print "$gi\t$version\t$cds\n"; $gi = $cds = undef; } } close INFILE;

Replies are listed 'Best First'.
Re^2: Read, match string and print
by sophix (Sexton) on Feb 08, 2010 at 07:37 UTC
    Ah, okay. I thought of it as follows. I initialized these variables in the beginning and set them to empty. In the end, I wanted to print if and only if all of these variables are assigned values (which were supposed to be read from the file). Thus, I thought it might work if I tell Perl to expect them as defined. But it did not work. :) Thank you for the code! (It did not occur to me that I should have set them as undef before going for any loop)
    5555555 123456789 join(11111..222222,333333..444444) 10101010 123456789 join(66666..7777777,888888..99999)
    It fails to put a tab between the very first and second elements. Do you know why this is the case?

      I don't know.    When I tried it, it worked.

        Thanks once again. Yes, it is working but it is getting the wrong input and hence prints out incorrectly.
        /protein_id="NP_12312" /db_xref="GI:7546536"
        Here. in order to get the GI number, I use this matching expression elsif ( /^\s*protein_id\S*\n\s*\Sdb_xref="GI:(\d+)/ ) but it is not matching. Am I doing some fundamental mistake with the matching operators?