When I try to extract data from a hash I've created, I get wrong results. I have a script that parses a configuration file and populates some hashes with parts of the contents. Writing the hash is done like:
} elsif($gateway_cluster) { #print "adding gwc :$addr: :$fqn: :$customer_name: :$object_name: : +$fw_ver:\n"; $cluster_info{$addr} = { fqn => "$fqn", customer_name => "$customer_name", cluster_name => "$object_name", fw_ver => "$fw_ver", } #} elsif.....
Later in the script I try to retrieve the information and get weird results. After trying everything I could think of to troubleshoot, I pasted in the following section 3 times:
print "##############################################################\ +n"; if(VERBOSE) { my $counter = 1; print "The cluster_info tables:\n------------------------\n"; while ( my ($IP, $hashref) = each %cluster_info) { printf "%-3s %-20s %-18s %s", "$counter", "cluster IP:", ":$I +P:", "\n"; for my $param ( sort keys %{ $hashref } ) { printf "%-20s %-18s %s", "$param" . ":", ":$cluster_info{$ +IP}{$param}:", "\n"; } $counter++; print "=====================================================\ +n"; } }
This printed the contents of the hash three times, with an entry counter; one printing after another. The 1st time had eight entries. The 2nd and 3rd were identical with 58 entries each, of which entries 51 - 58 corresponded to the entries 1 - 8 in the 1st printing. It is as if the first time through %cluster_info is getting accessed from the 51st position instead of the 1st. Then I changed the printing loop by adding this line at the top, right after setting the counter:
print "size of hash: " . keys( %cluster_info ) . ".\n";
Then everything became correct. This problem has been reproduced using recent 5.8.x versions of ActivePerl on Windows XP and 2000, and recent ActivePerl on Solaris 8, and using the most recent Cygwin with Perl on Windows 2000. What am I doing wrong?

In reply to Wrong hash output by mathew_

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.