Path: utzoo!attcan!uunet!decwrl!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Newsgroups: comp.lang.perl Subject: Re: #elements in assoc array / array in scalar context Message-ID: <8925@jpl-devvax.JPL.NASA.GOV> Date: 30 Jul 90 18:24:01 GMT References: <15429@thorin.cs.unc.edu> Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 19 In article <15429@thorin.cs.unc.edu> tell@oscar.cs.unc.edu () writes: : : What is the recommended way to find out how many elements are in an : associative array? I don't see any method that looks particularly : efficient. There isn't any efficient way. You can find out how many buckets in the hash table are in use, which will give you an approximation for non-dbm files, since most filled buckets will only have a single entry. I suppose I could keep a count, but I don't. And such a count wouldn't work on dbm files anyway. Currently, in a scalar context, %array returns the filled_buckets/allocated_buckets ratio. That's 0/4 on an empty associative array. After the next patch, it will return 0 if no buckets are filled, so you can at least use %array as a boolean. Given the problem with dbm files, I don't anticipate changing Perl to keep track. If you want the count, then $count++. Larry