Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!convex!usenet From: tchrist@convex.COM (Tom Christiansen) Newsgroups: comp.lang.perl Subject: Re: A question on associative arrays Message-ID: <1991May31.122026.8045@convex.com> Date: 31 May 91 12:20:26 GMT References: <1991May30.205555.14490@convex.com> <1991May31.011755.4305@eng.umd.edu> Sender: usenet@convex.com (news access account) Reply-To: tchrist@convex.COM (Tom Christiansen) Organization: CONVEX Software Development, Richardson, TX Lines: 51 Nntp-Posting-Host: pixel.convex.com From the keyboard of ziegast@eng.umd.edu (Eric W. Ziegast): :(Tom Christiansen) writes: :> From the keyboard of (Przemek Klosowski): :> How does one check if the associative array was assigned at all? :> :> if (%a) { :> :> will actually work, but I don't think that Larry has ever documented :> this. :Perhaps this should be documented. I gave it a few tests of wort-case :scenarios... : :Someone more knowledgable might want to explain why/how this works. Here's an article from Larry around 3700 articles ago explaining this: Date: 30 Jul 90 18:24:01 GMT From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Subject: Re: #elements in assoc array / array in scalar context Reply-to: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Organization: Jet Propulsion Laboratory, Pasadena, CA Newsgroups: comp.lang.perl 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 Sounds like he's documented it to me. :-) --tom -- Tom Christiansen tchrist@convex.com convex!tchrist "So much mail, so little time."