Hack: Get count of keys in a dict

Published: 2020-03-09 12:36:51 -0500 -0500

Reading time: 1 mins (93 words)

Tags: hack hacklang dict

problem

I’ve been using a lot more hacklang at work and as such have run into a lot of use cases that don’t seem to have too much documentation. One such use case is how to get the number of keys in a dict.

solution

The hacklang standard library is actualy pretty uniform so similar to other Traversable’s you might be trying to get the count of, you can get the count of keys in a dict with C\count.

For example:

$my_dict = dict[
    'a' => 1,
    'b' =>2
];
$my_count = C\count($my_dict);

About the Ham

Hi I'm Hamilton and I wrote / built this! I like doing lots of things, but mostly I like to build (and then share those buildings). Some things I'm currently building: art, software, and prose. To stay up-to-date with my latest buildings, subscribe to my email list or follow one of my many other accounts. #buildit #shipit #justhamit

comments powered by Disqus