Indeed, the culprit is the VERSION which appears only once and needs to be printed everytime along with the other two variables. So it should look like:
gi1 VERSION cds1 gi2 VERSION cds2 . . . . . . . . .
Okay, and here is the code:
#!/usr/bin/perl use strict; use warnings; my %info = ('gi' => "", 'version' => "", 'cds' => ""); # here we collect all information sub flush_info { # print out all information: print "$info{gi}"."\t"."$info{version}"."\t"."$info{c +ds}\n"; %info = (); }; my $data = '/DATA/GenBankFile.gb'; # GenBank file is located at C:\DAT +A open (INFILE, '<', $data) or die "Cannot!\n"; while (<INFILE>) { last if m!//$!; if (m!^VERSION.*\w:(\d+)! ) { $info{version} = $1; } elsif (m!GI:(\d+)!) { if ($info{cds}) { # we are in a CDS block $info{gi} = $1; }; } elsif (m!^\s+CDS\s+(.*)!) { # a new gene information has started flush_info(); # now remember the CDS $info{cds} = $1; } else {warn "Ignoring unknown line [$_]\n";}; }; # Output any leftover information: flush_info();
and for this file:
LOCUS NC_0000230 600020 bp DNA linear CON 21- +APR-2007 VERSION NC_000023.10 GI:123456789 CDS join(11111..222222,333333..444444) /db_xref="GI:55555555" CDS join(66666..7777777,888888..99999) /db_xref="GI:10101010" //
--this is what we get:
Ignoring unknown line [LOCUS NC_0000230 600020 bp D +NA linear CON 21-APR-2007 ] Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf16.pl line 12, <INFILE> line 5. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf16.pl line 12, <INFILE> line 7. 123456789 55555555 join(11111..222222,333333..444444) 10101010 join(66666..7777777,888888..99999)
First line is ignored as expected. Then it founds the VERSION and keep its value. Once it made its way to flush_info(), it gets printed without waiting for the other two variables, and becomes empty. Since there is no other VERSION for the remainder of the code, it never gets printed (well, it prints "").

I thought of two ways to overcome this problem, but either they did not work out or I did not implement them properly.

1. Within the sub routine, I introduced a next along with an if statement as follows:

sub flush_info { # print out all information: next print "$info{gi}"."\t"."$info{version}"."\t"."$inf +o{cds}\n" if ($info{gi} or $info{version} or $info{cds} eq undef; %info = (); };

Idea was to skip printing unless all three variables are defined. By doing this, I intended to solve the problem with the first line of output (i,e. only printing one variable)

2. Within the while loop, I tried to set the VERSION permanently, but I could not do it. I used matching =~ as an assignment and alternatively introduced a fourth variable to keep the value of the VERSION to use this value for printing purposes.

No progress. =) I am stucked once again, hooray!

In reply to Re^14: Read, match string and print by sophix
in thread Read, match string and print by sophix

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.