http://www.perlmonks.org?node_id=1002998


in reply to Re^6: Parsing problem
in thread Parsing problem

In the code I posted above, everything between the text "Here's exactly what I used to test your code and data:" and the [download] link is part of the one script. I suspect you tried to separate that into two files (script and data). Also, on this site, when code lines don't fit on one line they wrap to the next line - this is indicated by the addition of a plus (+) sign (which is coloured red by default). You don't want these additional plusses in your code; so, use the [download] link to get a plain text version. Save this plain text to a file - the usual filename extension for Perl scripts is ".pl" - the filename I used was pm_gene_uninit.pl (the following assumes you've used the same name). You can now run

perl pm_gene_uninit.pl

and, hopefully, you'll now get the expected output (displayed on the screen - not in a file).

Lines in Perl code that start with a hash (#) sign are comments: they are ignored by Perl when the script is run. [Exception: if the first line begins with #! it's not actually a comment - I think you can safely ignore that for now - see perlrun - #! and quoting on non-Unix systems for more information.] Adding a # to the start of a line of code is referred to as commenting out that line of code. Where I earlier referred to "removing the debug print statements", I actually commented them out, e.g.

#say qq(DEBUG: Line = "$line");

I also commented out all the lines relating to external files:

#my $file = "BSAC.pl"; ... #open my $in, "<", "$file"; #open my $out, ">", "output.txt";

and also changed these three lines:

say $out "Coordinate No of Strains AA Change"; ... while ( my $line = <$in> ) { ... printf $out "%-12.12s %-15.15s %s\n", $SNP, $count, $change;

to

say "Coordinate No of Strains AA Change"; ... while ( my $line = <DATA> ) { ... printf "%-12.12s %-15.15s %s\n", $SNP, $count, $change;

Removing $out from the say and printf statements means output now goes to the screen instead of a file (that's an oversimplification but will suffice for this discussion). Changing $in to DATA means the input is now everything following __DATA__ - see perldata (under Special Literals) for more details about this. Assuming that you did put everything after __DATA__ in a separate file, all of this explains "I get "Coordinates No of Strains AA Change" printed in my command line, and no output file created.".

That should get us back to: "Try running this. Assuming it works, try changing the data to something you know will generate the warnings - keeping the data to an absolute minimum.".

Going all the way back to your original posting, you have an input file called BSAC.pl and a script called Script.txt. I don't have a Perl running under MSWin to test this; however, I expect MSWin will interpret your input file as a Perl script and your actual Perl script as a plain text file. It's possible one (or both) of these may be related to your original problem. Try renaming your input file to BSAC.txt and your Perl script to Script.pl and see if you get better results.

I'd also recommend you read perlintro (a brief introduction and overview of Perl) and bookmark perl (which has links to all the Perl documentation).

-- Ken