in reply to Re: Re: Hash key counting
in thread Hash key counting
#!/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:
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.17 => 1 3 => 1
|
|---|