|Perl Monk, Perl Meditation|
Re: Re:x2 Counting keys with defined or undefined elements in a hashby jsprat (Curate)
|on Jun 05, 2003 at 17:32 UTC||Need Help??|
My aesthetic sense is somewhat offended by scanning the list twice using grep.
Mine too - as well as my common sense (no offense broquaint ;-)
Here's a quick benchmark of my first thought (&for_values), my second thought (&grep_subtract) your method and broquaint's double grep.
Using grep is deceptively fast - it looks like using the ternary operator in a single loop is slower than looping twice!
By far the fastest of these is using keys to find the total number of hash elements and subtract the number of defined elements.
I wonder how this would perform as the hash grows?
Update: Moved Benchmark results outside of readmore...