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: Intermediate:
Getting "uninitialized value" for declared hash values

 



sessmurda
Novice

Mar 14, 2013, 6:00 PM

Post #1 of 5 (757 views)
Getting "uninitialized value" for declared hash values Can't Post

I'm having an issue with a syntax I've used for a while to compare data within a hash while looping through a file. I provide samples of my data/scripts below, but basically my hash points to an arrary reference, which contains a list of data. In the past this has worked great, no warnings, but now I get not only a warning, but when I ask perl to match alphabetical values it says that they are uninitialized, but if I ask it to do anything for the numerical values they are empty. In order to get perl to match even the alphabetical values it needed quotes around the entire hash call, but this does not work for the numeric values.
First data file:

Code
Scaffold100015:59-110	1	52	mol-32-169f-137272_length=2968	157	208	100	-1	52 
Scaffold100015:768-819 1 50 mol-32-169f-137272_length=2968 217 266 100 -1 50
Scaffold100023:472-523 1 52 mol-32-169f-078323_length=4730 1032 1083 100 1 52
Scaffold100023:554-605 1 52 mol-32-169f-078323_length=4730 1114 1165 100 1 52
Scaffold100023:675-726 1 52 mol-32-169f-078323_length=4730 1224 1275 100 1 52
Scaffold100023:95-146 1 52 mol-32-169f-078323_length=4730 646 697 100 1 52

2nd data file:

Code
Scaffold100015:59-110 	Scaffold100015:768-819  
Scaffold100023:554-605 Scaffold100023:675-726
Scaffold100023:95-146 Scaffold100023:472-523

Script:

Code
use warnings; 
open (IN, "$ARGV[0]") || die "nope1\n";
open (IN2, "$ARGV[1]") || die "nope2\n";
my %data;
my %loci;
my %out;
while (<IN>)
{
chomp;
my @locus = split /\t/;
$loci{$locus[0]} = [ $locus[1], $locus[2], $locus[3], $locus[4], $locus[5], $locus[6], $locus[7], $locus[8] ];
}
close (IN);
while (<IN2>)
{
chomp;
my @fields = split /\t/;
my @Five = (split /:/, $fields[0]);
my @Three = (split /:/, $fields[1]);
my @Five_align = (split /-/, $Five[1]);
my @Three_align = (split /-/, $Three[1]);
my $Five_end = $Five_align[1];
my $Three_beg = $Three_align[0];
my $inferred_gap_size = ($Three_beg - $Five_end);
# print "$inferred_gap_size\n";
# print "$fields[0]\t$fields[1]\n";
if ( (defined "$loci{$fields[0]}") && (defined "$loci{$fields[1]}"))
{
# print "Hey\n";
if ("$loci{$fields[0]}[2]" eq "$loci{$fields[1]}[2]")
{
my $Mer_gap_beg = ($Five_end + 1);
my $Mer_gap_end = ($Three_beg - 1);
my $Scaffold = $Five[0];
my $MGB = $loci{$fields[0]}[4];
my $MGE = $loci{$fields[1]}[3];
my $Mol_gap_beg = ($MGB + 1);
my $Mol_gap_end = ($MGE - 1);
my $moleculo_gap_size = ($Mol_gap_beg - $Mol_gap_end);
my $gap_diff = ($inferred_gap_size - $moleculo_gap_size);
my $gap_ratio = ($inferred_gap_size/$moleculo_gap_size);
$out{"$Scaffold:$Mer_gap_beg:$Mer_gap_end"} = [ "$loci{$fields[0]}[2]", $Mol_gap_beg, $Mol_gap_end, $moleculo_gap_size, $inferred_gap_size, $gap_diff, $gap_ratio ];
}
}
}
close (IN2);
foreach my $name (sort keys %out)
{
my ($read, $gap_beg, $gap_end, $mo_gap_size, $inf_gap_size, $tgap_diff, $tgap_ratio) = @{$out{$name}};
print "$name\t$read\t$gap_beg\t$gap_end\t$mo_gap_size\t$inf_gap_size\t$tgap_diff\t$tgap_ratio\n";
}

I should start by saying the note lines are where I was testing to make sure everything was declaring correctly, with the exception of the "loci" hash within the 2nd file everything works fine. The first loop opens the first file and slurps everything into a hash, first column always unique so there are no issues. Earlier I troubleshooted looping through the hash and printing values between the files and that worked, so the initial declaration doesnt seem to be the problem. Next I open the 2nd file and do some comparisons/declarations. The "if defined" statement will only return true if I put quotes around the hash, I've never had to do this before for the same type of hash, and it still says the values are uninitialized, so perhaps they are actually empty? When I print out the "$fields" lines before they exactly match the key, so I am unsure why they would be uninitialized. Later in the MGB/MGE lines I get that uninitialized values are being used in numerical comparisons. I know I must be missing some simple declaration syntax, but I am confused about why I've been able to process other data sets with this syntax and it fails here. Thanks ahead of time for any advice, sorry for the long post, haven't posted here in a while and wanted to be sure to be clear.


Chris Charley
User

Mar 14, 2013, 6:48 PM

Post #2 of 5 (754 views)
Re: [sessmurda] Getting "uninitialized value" for declared hash values [In reply to] Can't Post

Don't know if this helps, but try printing:

Code
print "<$fields[0]>\t<$fields[1]>\n"; 
instead of:
print "$fields[0]\t$fields[1]\n";

This should show clearly if the @fields array contains what it should.

I cleaned up input files - some weren't separated by a single tab, perhaps in your data that might be the case. When I did that, your program ran without errors and produced this output.

Code
Scaffold100015:111:767	mol-32-169f-137272_length=2968	209	216	-7	658	665	-94 
Scaffold100023:147:471 mol-32-169f-078323_length=4730 698 1031 -333 326 659 -0.978978978978979
Scaffold100023:606:674 mol-32-169f-078323_length=4730 1166 1223 -57 70 127 -1.2280701754386



sessmurda
Novice

Mar 14, 2013, 7:18 PM

Post #3 of 5 (748 views)
Re: [Chris Charley] Getting "uninitialized value" for declared hash values [In reply to] Can't Post

Thanks, pretty rookie mistake on my part, also that is a good tip on the testing for whitespace, will help in the future.


sessmurda
Novice

Mar 14, 2013, 7:37 PM

Post #4 of 5 (743 views)
Re: [Chris Charley] Getting "uninitialized value" for declared hash values [In reply to] Can't Post

Wait, now when I run the script I am getting a slightly different output.

Code
Scaffold100015:111:767	mol-32-169f-137272_length=2968	209	216	-7	658	665	-94 
Scaffold100023:147:471 mol-32-169f-078323_length=4730 698 1031 -333 326 659 -0.978978978978979
Scaffold100023:606:674 mol-32-169f-078323_length=4730 1166 1223 -57 70 127 -1.2280701754386
Scaffold100024:561:791 1 -1 2 232 230 116
Scaffold100032:1136:1465 1 -1 2 331 329 165.5
Scaffold100032:15590:15676 1 -1 2 88 86 44
Scaffold100032:24777:24990 1 -1 2 215 213 107.5
Scaffold100032:25815:26203 1 -1 2 390 388 195

Where the bottom lines with no "mol" in the 2nd column are not defined in the hash and shouldnt be returned. Also when I turn warnings on I get the uninitialized message again. I would let it run on the data set and simply grep those lines but then it complains of a division by 0. Any pointers?


Laurent_R
Veteran / Moderator

Mar 15, 2013, 12:48 PM

Post #5 of 5 (728 views)
Re: [sessmurda] Getting "uninitialized value" for declared hash values [In reply to] Can't Post

It seems that the $moleculo_gap_size variable is probably equal to 0, meaning that $Mol_gap_beg is probably equal to $Mol_gap_end.

Try to print your variables to find out what is going on.

 
 


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

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