in reply to Re: Hash key counting
in thread Hash key counting

Ver nice response Limbic, my apologies for being unclear. I've replied to BrowserUK above and hope the explanation sheds some light on my problem.

The points you've made above are exactly what I've intended the code to do.

I have checked the results as you have suggested by using print, what seems to happen is that the result of $key_num seems to stay the same and it works for the last iteration of the loop, the last key number in %columns is always incremented by one, though it seems that no matter how many times I loop, the last inserted key is overwritten, seems to me (I used print) that $key_num always returns the same number.


BlackJudas

Replies are listed 'Best First'.
Re: Re: Re: Hash key counting
by Limbic~Region (Chancellor) on Apr 21, 2003 at 21:57 UTC
    blackjudas,
    The following code works fine for me - just as you want it to:
    #!/usr/bin/perl -w use strict; my %columns = ( 1 => 1, 2 => 1, 3 => 1, 4 => 1 ); my @discounts = qw( 1 2 1 3 1 4 1 5); for my $discount (@discounts) { if ($discount == 1) { my $key_num = scalar keys %columns; $key_num++; $columns{$key_num} = 1; } } print "$_ : $columns{$_}\n" foreach (keys %columns);

    Cheers - L~R

    Update: As others have pointed out - if %columns is already populated and the keys are not sequential starting from one - you run the risk of this not working. This could also be a problem if you delete keys elsewhere. For instance:

    17 => 1 3 => 1
    There are 2 keys, add one you get 3, but guess what - 3 already exists. You may want to check to see if the key exists. I think there is a better way to accomplish whatever you are trying to do.
Re^3: Hash key counting (missing key)
by tye (Sage) on Apr 22, 2003 at 16:04 UTC

    Then it is quite likely that you a missing one key value (as has been hinted at several places in this thread). What keys are in the hash when this code isn't working? If the keys are, for example, 1, 2, 3, 5, 6, and 7, then you have 6 keys and you will use "7" as your next key (since "4" is missing). Such would explain your problem.

    This is part of why so many have complained about not understanding your code. Part of your code assumes that all keys from 1 through N will be present (and no others) which means an array would be a more appropriate data structure (and wouldn't be susceptible to this type of bug). And you have yet to explain how you use this hash elsewhere, so we are forced to guess why you'd choose what appears to be a very poorly fitting solution, so we can't help much with fixing it.

                    - tye