Search code examples
arraysfunctionperlmodule

I am not able to get any values to store in the array @AA


It is getting the correct inputs and printing them inside the for loop but when I try to send it to a function module later or if I try to print it outside the for loop it is empty. What do I need to change?

#!/usr/bin/perl
use lib "."; # This pragma include the current working directory 
use Mytools;


$inputfilename = shift @ARGV;
open (INFILE, $inputfilename) or die
            ("Error reading file $inputfilename: $! \n");

# Storing every line of the input file in array @file_array
while (<INFILE>){
        $file_array[ $#file_array + 1 ] = $_;
        
}
my $protein;
my @AA;
foreach $protein (@file_array)
{
@AA = Mytools::dnaToAA($protein);
print "The main AA\n",@AA;
}
print "The main array",@file_array;

my $header1 = "AA";
my $header2 = "DNA";
Mytools::printreport($header1, $header2, \@AA, \@file_array);

Solution

  • You're overwriting the @AA in every iteration of the foreach loop. Instead of

    @AA = Mytools::dnaToAA($protein);
    

    use

    push @AA, Mytools::dnaToAA($protein);
    

    See push.

    Next time, try to post runnable code (see mre), i.e. avoid Mytools as they're irrelevant to the problem and make the code impossible to run for anyone else but you.