In one block of code I fill up a variable, which happens to be a hash of hashes of hashes .... In the next block of code, I go to use it. The variable has disappeared. The variable is a hash called "%type_normalName_parent1_parent2_index_measured". As I fill it up, I print it out to make sure everything is going as I think:

It prints out just fine. If I put a block (to stop execution in a debugger) anywhere in that block of code, I see the variable and everything looks normal. However, if I put that block just outside of the block of code where I fill the variable, there is no more variable. It's just gone. Any ideas for what's going on? I've attached the code, for what it's worth (which I expect is nothing).

Can you be more specifc about where the problem is in that code you posted?

Declaring all your variables as global variables is not the way to write good code. Variables should be scoped to just the section of code they are used in. -------------------------------------------------

Can you be more specifc about where the problem is in that code you posted?

Declaring all your variables as global variables is not the way to write good code. Variables should be scoped to just the section of code they are used in.

Sorry for the very late response to this.

I almost always declare all of my variables globally because I feel like it minimizes the chance of my making mistakes. Perhaps I should rethink that philosophy, but I'm quite sure that it's not what is causing me problems.

I just ran in to this same problem again in an entirely unrelated script. Here I'm making a hash of hashes (not sure if that's relevant to this or if it could happen with just any variable) and it disappears. This time I've put a debugger "block" in outside of the block of code where I filled the variable and everything is still fine - the variable is there and filled as I had intended. Then I start stepping through the code in the debugger and at one completely innocuous step (opening a file or writing something to a file), my stored variable just disappears. It's not that it empties out and is filled with nothing - it's just gone. It's not even there in the debugger. And when I run the script from a terminal, I get the same result. It seems to me that this has to be a problem with the interpreter. I don't see what else it could possibly be, although I'm certainly aware that mistakes generally lie with the person writing the code rather than some widely distributed tool. (I'm using the perl that came on my Mac OS X Leopard.)

I have attached all of my code, though I don't expect that this will help anyone. Really the question is: How can a variable disappear when you click one step through on your debugger and that step has absolutely nothing to do with the variable that disappears?

Now the variable called "%type_normalName_cutoff_chromosomes" is gone.

This isn't the only instance where this happens - if I continue stepping through the code, another one disappears. So I'd love to get an answer to this.

I'd also appreciate hearing your other criticisms of my code. I'm self taught and write this code for work in a laboratory where none of my colleagues know anything about programming. (They're bench scientists, which is what I was before I taught myself some programming.) I'm sure there are all sorts of ways that my coding practices can be improved and I'd very much appreciate hearing about them.

So, %type_normalName_cutoff_chromosomes is being loaded on line 137 and the next time it's accessed is on line 194 and finally on line 195.

The print statement you referenced is on line 226.

Are you saying that when line 194 is executed, the hash is no longer assigned?

Have you tried using Data::Dumper to see what it holds and its structure at each point?

No. When line 194 is executed, the hash is still there and everything is fine. When I block in my debugger before line 224 is executed, the hash is still there and everything is fine. Then when I execute line 224 (just stepping to the next line, which is line 226, and not yet executing that one), then the hash has vanished into thin air.

Since the var is never used beyond line 195, why are you concerned about its status when you get to line 226.

I suspect that part of the problem may be your misunderstanding of the debugger.

I'm concerned because if the variable doesn't exist, I'll never get past this statement, which is after line 226:

if ($type_clique_cutoff_chromosomes{$type}{$clique}{$cutoff}) {

This is not an issue of my not understanding my debugger. For example, I usually run my programs from a terminal while I'm writing them in the debugger, and I get the same result that way (i.e. I'll end up with output files that have a header line and then nothing else, which is a consequence of not getting into the "if" statement above).

Can you post the console output of the debug session and indicate how you determined that the var "disappears"?

I'll paste the console output below. I determined that the variable disappeared because:

1. If I just look at the variable in my debugger, it is initially there, filled correctly (as it is in the output below) and then I execute one line that's unrelated to the variable and it's then gone.

2. I never get into the "if" statement mentioned above. I have the "if" statement there because only some combinations of "types", "cliques" and "cutoffs" will have "chromosomes" associated with them, and it's only those where there will be things to print out to my output file.

3. As expected if the variable disappears, I don't print anything to my output file other than the first line.

As I mentioned above, this happens to another variable too. It doesn't disappear at the same line, but it does disappear a little later. I think this must be a problem with the interpreter. I'll try to run it on a different machine.

OK, I think I'm figuring this out. I think I'm misusing "foreach" somehow. For example the code below prints everything out fine except for the final

print "and finally file is $file\n";

Then it doesn't have the variable "$file" any more. When I'm going around in the "foreach" loop, it is treating "$file" as whatever element of @array it's dealing with and not as some independent variable called "$file". The code I linked to is filled with "foreach" loops, and I'm sure I'm making this mistake all over the place. And that, I expect, is somehow leading to this weird behavior.

OK, this is now getting really bizarre. I fixed up a bunch of potential screw ups with "foreach" loops, though I'm not convinced that I made a mistake there. Then I tried again - it still doesn't work. Then I filled the variable I've been having trouble with and commented out all the rest of the code until the final line, which says:

print "done!\n";

I put a block before that line and run it and ALL of my variables are gone. Then before the commented out block of code, I put in this line:

print "check now\n";

I put a block before that and now all of my variables are there and filled correctly and everything is fine. So somehow printing "check now" and jumping over a big block of commented out code has led me to lose all of my variables. Removing the commented out code fixes the problem - then when I put a block before the final "done" line, all the variables are there.