I have a program that creates an array of hashes while parsing a FASTA file. Here is my code
use strict;
use warnings;
my $docName = "A_gen.txt";
my $alleleCount = 0;
my $flag = 1;
my $tempSequence;
my @tempHeader;
my @arrayOfHashes = ();
my $fastaDoc = open(my $FH, '<', $docName);
my @fileArray = <$FH>;
for (my $i = 0; $i <= $#fileArray; $i++) {
if ($fileArray[$i] =~ m/>/) { # creates a header for the hashes
$flag = 0;
$fileArray[$i] =~ s/>//;
$alleleCount++;
@tempHeader = split / /, $fileArray[$i];
pop(@tempHeader); # removes the pointless bp
for (my $j = 0; $j <= scalar(@tempHeader)-1; $j++) {
print $tempHeader[$j];
if ($j < scalar(@tempHeader)-1) {
print " : "};
if ($j == scalar(@tempHeader) - 1) {
print "\n";
};
}
}
# push(@arrayOfHashes, "$i");
if ($fileArray[$i++] =~ m/>/) { # goes to next line
push(@arrayOfHashes, {
id => $tempHeader[0],
hla => $tempHeader[1],
bpCount => $tempHeader[2],
sequence => $tempSequence
});
print $arrayOfHashes[0]{id};
@tempHeader = ();
$tempSequence = "";
}
$i--; # puts i back to the current line
if ($flag == 1) {
$tempSequence = $tempSequence.$fileArray[$i];
}
}
print $arrayOfHashes[0]{id};
print "\n";
print $alleleCount."\n";
print $#fileArray +1;
My problem is when the line
print $arrayOfHashes[0]{id};
is called, I get an error that says
Use of uninitialized value in print at fasta_tie.pl line 47, line 6670.
You will see in the above code I commented out a line that says
push(@arrayOfHashes, "$i");
because I wanted to make sure that the hash works. Also the data prints correctly in the desired formatting. Which looks like this
HLA:HLA00127 : A*74:01 : 2918
try to add
print "Array length:" . scalar(@arrayOfHashes) . "\n";
before
print $arrayOfHashes[0]{id};
So you can see, if you got some content in your variable. You can also use the module Data::Dumper to see the content.
use Data::Dumper;
print Dumper(\@arrayOfHashes);
Note the '\' before the array!
Output would be something like:
$VAR1 = [ { 'sequence' => 'tempSequence', 'hla' => 'hla', 'bpCount' => 'bpCount', 'id' => 'id' } ];
But if there's a Module for Fasta, try to use this. You don't have to reinvent the wheel each time ;)