File I/O overwrite array variables?

J

jamesrwagner

Hello:

I was basically finding that after a block of code, an array I had
called @localizations was still the same size, but each of the
variables was now being overwritten by an empty string. What I found
on closer inspection was that as I was reading in from the file with
handle INPUT, each line was in turn being stored in the current place
that it was on from the outer foreach loop, until upon termination
when the final line is only a newline storing a n empty string in that
place holder.

The code below


sub indexSequences {

my $fold = shift;
my $minSup = shift;

my @localizations = ("cytoplasmic", "cytoplasmicmembrane",
"extracellular",
"outermembrane", "periplasmic");
my %containsHash;

foreach(@localizations) {
my $localization = $_;
open INPUT, "freseqs" . $_ . $fold . $minSup ;

while(<INPUT>) {
chomp;




}

foreach(@localizations) {
my $alocalization = $_;
print("This is the $alocalization $_\n");
}
}


}
 
P

Peter Makholm

foreach(@localizations) {

Here you make $_ an alias to each element of @localizations
my $localization = $_;
open INPUT, "freseqs" . $_ . $fold . $minSup ;

while(<INPUT>) {

Here you modify $_ which, as we remember, is an alias into
@localizations. So it is quite expected that you in later iterationes
over @localizations sees these modified values.

//Makholm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top