BlackJudas,
Like Aristotle, I do not understand what you are trying to accomplish, so let me explain in plain English what your code is doing so maybe it will help

  • You loop through every value of the @discounts array
  • If any value is numerically equivalent to 1, you get the current number of keys in %columns
  • You then add one to that number
  • Finally, you create a new entry in %columns whose key is the previous number of keys + 1, and the value is set to 1

    If you are expecting the %columns to increase then you may want to add a print statement that says:
    print "Discount == 1\n";
    Then check to see if this is happening as often as you expect.

    Note: If you are not going to use the value of the hash, you could save time by setting it to undef as in
    $columns{$key_num} = undef;
    And if you are going to do that, you might as well use an array instead of a hash, but there is probably something else you are using it for.

    I hope this explains what is going on.

    Cheers L~R

    Update: Modified entire reply as I realized the problem is probably with the outside loop


    In reply to Re: Hash key counting by Limbic~Region
    in thread Hash key counting by blackjudas

    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.