
sessmurda
Novice
Mar 14, 2013, 6:00 PM
Post #1 of 5
(273 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:
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:
Scaffold100015:59-110 Scaffold100015:768-819 Scaffold100023:554-605 Scaffold100023:675-726 Scaffold100023:95-146 Scaffold100023:472-523 Script:
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.
|