CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [MB123] File parsing problem, Use of uninitialised value error.: Edit Log



Laurent_R
Veteran / Moderator

Nov 11, 2012, 2:59 PM


Views: 9759
Re: [MB123] File parsing problem, Use of uninitialised value error.

Hi,

I tried to run the code on your data and I did not get your warning message about uninitialized value.

The output file also seems OK to me:


Code
$ cat output.txt 
Coordinate No of Strains AA Change
27534 1 AA Leu->Ser
27710 2 AA Val->Phe
27771 1 AA His->Arg
28047 1 AA Lys->Ile
28490 2 AA Gly->Cys
28548 1 AA Ser->Leu
28787 1 AA Asp->Asn
28941 1 AA Gln->Arg
29214 1 AA Val->Ala
29574 1 AA Pro->Leu


So presumably your data is larger than what you showed us and somewhere else down the input file, some part of the data is not consistent with the data segment you gave us.

Your warning message says:


Code
Use of uninitalized value ($count or $change) in concatenation (.) or string at Script.pl line 24, <IN> line XXXX.


The "line XXXX" part (XXXX is presumably a number) says where in the input file the error occurred. Locate the first of these lines number XXXX in the input file and look carefully at the group of three lines in the file before that line.

If you don't see anything wrong, post these lines, I or someone else might find out.

Some advice concerning usually accepted better programming practices:
- Always "use strict;"
- Always "use warnings;"

These two diagnostic pragmas will tell you a lot about possible errors in your program, often even before it runs. They will force you to declare your variables ("my" statement) and to think about where they need to exist.

-Always check the return value of system calls such as open file
- Use the more modern syntax to open your files (see example below).

So, this is quick rewrite of your script with such advice in mind (plus a little bit of reformatting for clarity, but you don't need to agree with me on the reformatting):


Code
use warnings; 
use strict;

my %cod;
$cod{1} = "Int";
$cod{2} = "non";
$cod{3} = "syn";
$cod{4} = "stop";

my $file = "input.txt";
open IN, "<", $file or die "could not open $file $! \n";
open OUT, ">", "output.txt" or die "could not open output.txt $! \n";
print OUT "Coordinate No of Strains AA Change\n";
my ($SNP, $count, $change);
while(<IN>){
if (m/^FT\s+SNP\s+(\d+)/) {
$SNP = $1;
}
elsif (m/^FT\s+\/note="(.*)"/) {
my $line = $1;
$count = ($line =~ tr/=/=/);
$line =~ m/\((AA \w+->\w+)\)\s*$/;
$change = $1 || "";
}
elsif (m/^FT\s+\/colour=(\d+)/) {
print OUT "$SNP $count $change\n" if $cod{$1} eq "non";
}
}


One final additional point: I don't think the hash at the beginning is very useful since you are checking $cod{$1} only against "non", which will match only if $1 = 2, so that you could have your conditional statement:

print OUT "$SNP $count $change\n" if $1 == 2; # or : if $1 eq '2';

and could forget altogether about the %cod hashtable (unless of course you showed us only part of your code).


(This post was edited by Laurent_R on Nov 11, 2012, 3:01 PM)


Edit Log:
Post edited by Laurent_R (Veteran) on Nov 11, 2012, 3:01 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives