in reply to Count of HoA elements
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Count of HoA elements
by Jenda (Abbot) on Jan 19, 2007 at 20:34 UTC | |
|
Re^2: Count of HoA elements
by BrowserUk (Patriarch) on Jan 19, 2007 at 20:29 UTC | |
by ikegami (Patriarch) on Jan 19, 2007 at 20:31 UTC |