Logo

Programming-Idioms

  • C#
  • Perl

Idiom #170 Get map size

Set n to the number of elements stored in mymap.

This is not always equal to the map capacity.

my $n = keys %mymap;

In scalar context, the keys operator returns a count of the keys in the hash. BUT, a hash can contain other hashes, as well as lists, nested arbitrarily. This only counts the top elements.
n = mymap.Count;
(def n (count mymap))

New implementation...
< >
programming-idioms.org