in reply to Hash key counting

$key_num always has the same value because you are always setting it to the same value. You are first setting it to the number of keys in %columns $key_nums = keys %columns;, and then you increment it by 1 $key_nums++;. You do this for every value in @discounts that has a value of 1?

It's completely unclear from your question what it is that you are actually trying to do....a written description of your requirement would probably clarify this.


Examine what is said, not who speaks.
1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
3) Any sufficiently advanced technology is indistinguishable from magic.
Arthur C. Clarke.

Replies are listed 'Best First'.
Re: Re: Hash key counting
by blackjudas (Pilgrim) on Apr 21, 2003 at 21:44 UTC
    The explanation of what I'm trying to do is going to be somewhat unclear, it is part of a database handler which parses output and creates HTML tables based on user selections. The %columns hash contains descriptors which (if valid) are parsed and a table is put together at a later time.

    I have gone over the logic, the @discounts array contains a number of elements which have either a value of 0 or 1. If the current element has a value of 1, then count how many keys are in columns.

    %columns = ( 1 => 1, 2 => 1, 3 => 1, 4 => 1 );


    In this case, I ask for how many keys are in %column, increment by one and move forward, so the above should return 4 += 1 = 5.

    Then assign:

    $columns{5} = 1;

    Loop again, return a count of 5, increment and assign:

    $key_num = keys %columns # 5 $key_num++; # 6 $columns{6} = 1;


    Is that a little more clear?

    BlackJudas