It is amazing to me how many times the most intractable problem is not caused by insufficent knowledge, or error in algorithmic design. No, the greatest culprit of all is Abject Stupidity. Fortunately, although the problems caused by stupidity are incredibly embarrassing, they can be quite educational. So I will swallow my pride, and share with you the story of my humbling idiocy.

Many of you have been following the ongoing saga described here and here. Many of you have been extremely helpful with your advice, and I thank you. Perlmonks is an endless supply of generosity and knowledge.

(Brief summary for people to lazy to read the nodes: I had a globally declared hash, which I was pushing references of onto an array. To make sure each reference was unique, I copied it to an anonymous hash first. But when I went through the array, I found that all of the elements were identical, with the values of whatever was put into the global hash last)

The strange thing was, after putting such generosity and knowledge to use, the problem remained! Try as I might to figure out the cause, I was beyond my knowledge. I tried asking vague questions, desperately fishing the brains of my fellow monks for a reason, any reason, why this might be happening.

Do you want to know what I finally discovered to be the cause? Here is a brief code sample, see if you can find it...

my %globalHash; my @globalArray; # do stuff with %globalHash, and push a snapshot of it # periodically onto @globalArray. .... foreach my $hashRef ( @globalArray ) { my %localHash = %{$hashRef}; print $globalHash{"KeyOne"}; print $globalHash{"KeyTwo"}; ... }
After all of that work, all of that brain-wracking, forehead-smacking, eye-straining, computer-cursing effort trying desperately to figure out why the last values of %globalHash were the ones held in each element of the array, what do I find? That there was nothing wrong at all with my array, or my calls to push, or my use of references, or anonymous hashes.

What was my error? I looped through each hash reference on my array, dutifully dereferenced it, and promptly printed out the contents of the other freakin' hash! On all of the complex and difficult tasks, I had programmed perfectly. It was the print statement that I did wrong.

There is a lesson in this, my fellow monks. When you've gone over a piece of code a dozen times searching for an error without finding it, when you've tried every variation of that code and still it fails in the exact same way, when you're absolutely positive that you're not doing anything wrong in that little scrap of code...you're probably right. It's a different little scrap of code that you're ignoring completely that is causing the problem.

Also, always check your print statements.

Thank you to everyone who helped me with this. And runrig, chromatic, yoda, and everyone else who warned me about the evils of global variables, just stop you're snickering! Just one more piece of knowledge gained through abject stupidity.

Some people drink from the fountain of knowledge, others just gargle.


In reply to Knowledge gained through abject stupidity by DeusVult

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.