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;
In reply to Re: Read, match string and print
by jwkrahn
in thread Read, match string and print
by sophix
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |