should do it
No, it shouldn't. @{...} expects that ... retunrns a reference to an array. values %hash does not.
anyway short of iterating through the entire hash.
What do you think values does? Returns a list obtained by iterating through the entire hash.
And whatever processes the list returned by value has to iterate over that list.
These work:
# time = O(N) memory = O(N*M) my $count = map @$_, values %h;
# time = O(N) memory = O(N) my $count = sum map { scalar @$_ } values %h;
# time = O(N) memory = O(N) my $count = 0; $count += @$_ for values %h;
# time = O(N) memory = O(1) my $count = 0; while (my ($k, $v) = each(%h)) { $count += @$v; }
Update:
Added 2nd and 4th solution.
Added time and memory analysis.
Choose the one that's the most readable and maintainable within your speed and memory requirements.
In reply to Re: Count of HoA elements
by ikegami
in thread Count of HoA elements
by sweetblood
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |