in reply to No: of values in a hashtable
Accordingly, scalar @{ $weekly_hash{$i} } will return the number of values:
$ perl -Mstrict -Mwarnings -E ' my %weekly_hash; my $i = 1; push @{ $weekly_hash{$i} },"mon","ret","tue","retik","wed","true"; say scalar @{ $weekly_hash{$i} }; ' 6
Your code shows no undefined values. Your text has "... the number of values ...". An undefined value is still a value:
$ perl -Mstrict -Mwarnings -E ' my %weekly_hash; my $i = 1; push @{ $weekly_hash{$i} },"mon","ret","tue","retik","wed","true", und +ef; say scalar @{ $weekly_hash{$i} }; ' 7
If you really meant that you only wanted a count of defined values, you can do this:
$ perl -Mstrict -Mwarnings -E ' my %weekly_hash; my $i = 1; push @{ $weekly_hash{$i} },"mon","ret","tue","retik","wed","true", und +ef; say scalar @{[ grep { defined } @{ $weekly_hash{$i} } ]}; ' 6
The count of elements in an array is always available via scalar; there's no need to introduce temporary count variables.
-- Ken
|
|---|