in reply to Re^8: Read, match string and print
in thread Read, match string and print

Nowhere in that source code does the word value appear, so I highly doubt that the program ever outputs the string Ignoring unknown value. I put the following line into the program:

warn "Ignoring unknown line [$_]\n";

in there so you know what parts of the input get discarded. In that line, I even output the part that gets discarded. If you determine that a certain line is never of use, just add another rule ignoring it.

Replies are listed 'Best First'.
Re^10: Read, match string and print
by sophix (Sexton) on Feb 08, 2010 at 10:38 UTC
    So I turned off the warnings
    #!/usr/bin/perl use strict; use warnings; my %info; # here we collect all information my @columns = qw(gi version cds); # The name and order of the columns +we want to print my $data = '/PRBB/Practice.gb'; # GenBank file is located at C:\PRBB open INFILE, '<', $data or die "Please insert a new coin!\n"; while (<INFILE>) { if (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 } elsif (m!^VERSION.*\w:(\d+)! ) { $info{ version } = $1; #} else {warn "Ignoring unknown line [$_]\n";}; };} # Output any leftover information: flush_info(); sub flush_info { # print out all information: print join '*', @info{@columns}; # and forget the collected information %info = (); };
    This gives an error at print join '*', @info{@columns}; And it prints all the matchings into one single line. How is it possible to print them out as would be in the following structure:
    if ( defined $cds && defined $gi && defined $version ) { # Print only +when all variables are defined print "$gi\t$version\t$cds\n"; $gi = $cds = undef; # Get ready for the next loop }
    Thanks for sparing time!

      If comparing two print statements and then modifying one so it works like the other exceeds your current skills, I recommend learning some more Perl. It's not really hard:

      @info{ @columns } is equivalent to $info{ gi }, $info{ version } and $info{ cds }, because that's what @columns contains.

      The join function joins a list with a string. I chose * as the string, but you might want to choose a different separator.

        No doubt I should learn Perl more (and programming in general). I guess I managed to handle the print statement. Now, the code is:
        #!/usr/bin/perl use strict; use warnings; my %info; # here we collect all information my @columns = qw(gi version cds); # The name and order of the columns +we want to print my $data = '/DATA/GenBankFile.gb'; # GenBank file is located at C:\DAT +A open INFILE, '<', $data or die "Cannot!\n"; while (<INFILE>) { if (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 } elsif (m!^VERSION.*\w:(\d+)! ) { $info{ version } = $1; #} else {warn "Ignoring unknown line [$_]\n";}; };} # Output any leftover information: #flush_info(); sub flush_info { # print out all information: print "$info{gi}"."\t"."$info{version}"."\t"."$info{cds}\n"; # and forget the collected information %info = (); }; >
        And here is some part of the output:
        Use of uninitialized value $info{"gi"} in string at C:\Perl\bin\wtf14. +pl line 32, <INFILE> line 154. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 154. Use of uninitialized value $info{"cds"} in concatenation (.) or string + at C:\Perl\bin\wtf14.pl line 32, <INFILE> line 154. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 180. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 206. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 232. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 258. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 284. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 314. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 345. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 374. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 404. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 434. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 481. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 498. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 534. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 600. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 617. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 634. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 667. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 710. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 721. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 746. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 773. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 836. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 848. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 860. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 892. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 938. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 952. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 996. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 1009. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 1046. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 1070. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 1098. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 1131. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 1174. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 1188. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 1216. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 1256. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 1269. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 1329. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 1344. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 1360. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 1403. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 1415. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 1445. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 1490. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 1505. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 1533. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 1591. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 1606. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 1621. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 1710. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 1767. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 1781. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 1795. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 1827. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 1865. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 1899. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 1934. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 1982. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 1998. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 2048. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 2083. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 2119. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 2264. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 2282. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 2300. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 2337. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 2382. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 2419. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 2450. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf14.pl line 32, <INFILE> line 2459. 30061484 join(68351..68408,76646..77058) 30061486 join(123270..123327,126056..126333) 83582801 join(138186..138234,139415..139665) 30061487 complement(join(168527..168759,170216..170264)) 197245446 join(207930..207987,209919..210412) 23943927 join(238420..238477,239718..239947) 55769555 complement(join(251848..251908,256609..256727, 21264338 278228..279439 197245445 306569..307516 41327717 join(330288..330476,333854..334279) 144953899 join(368655..368945,371931..372223,376842..377334) 5454168 join(389383..389423,398170..398263,398376..398574, 111185943 join(389402..389423,390525..390669,398170..398263, 47419901 complement(join(419230..419485,419752..419939, 4503095 complement(join(464605..464720,467020..467106, 29570791 complement(join(464605..464720,467020..467106, 48255908 complement(join(464605..464720,467020..467106, 22129777 complement(join(585235..585309,590357..590881)) 22129778 complement(join(629358..629561,633620..633829)) 197245441 join(629536..629555,633613..634228,642601..643113) 156564358 complement(join(644315..645105,656113..656245)) 108389007 complement(join(741670..741882,742345..742468, 46391104 825448..826335 108389127 825448..826335 21328453 825448..826335 83722283 complement(join(853603..853763,854927..855057, 90970328 complement(join(941000..941109,947817..947957, 145611425 complement(join(941000..941109,944578..944763, 145611430 join(1099417..1099545,1106141..1106293,1108069..11081 +51,