home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!usc!cs.utexas.edu!uwm.edu!ogicse!orstcs!prism!jacobsd
- From: jacobsd@prism.cs.orst.edu (Dana Jacobsen)
- Newsgroups: comp.lang.perl
- Subject: fast way to get number of elements in an assoc array?
- Summary: is there a way to count elements in an assoc array better than keys?
- Keywords: associative array number count $#array $#
- Message-ID: <1992Aug27.032106.20515@CS.ORST.EDU>
- Date: 27 Aug 92 03:21:06 GMT
- Article-I.D.: CS.1992Aug27.032106.20515
- Sender: usenet@CS.ORST.EDU
- Organization: Oregon State University, Computer Science Dept
- Lines: 29
- Nntp-Posting-Host: prism.cs.orst.edu
-
-
- Is there a function akin to $#array that works on associative arrays? I
- have an associative array with over 30,000 keys, and this is what I'm
- currently doing:
-
- @dummykey = keys(%records);
- print $#dummykey+1, " records loaded.\n";
- undef (@dummykey);
-
- The problem with this is that the key list takes up a significant chunk of
- memory, as well as being very slow. The alternative way of handling this is
- to keep a seperate variable that contains the number of elements in the
- array, something like:
-
- $records{$name} || $numberofrecords++;
- $records{$name} .= $; . $citekey;
-
- However, this seems unaesthetic, as well as raising the possibility (if records
- got added elsewhere in the code also) of having an incorrect count.
-
- Any ideas?
- --
- Dana Jacobsen "After learning Perl, I can't wait for Swine!"
- jacobsd@cs.orst.edu
- jacobsd@solar.cor2.epa.gov Power, Corruption, & Lies -- Bush '92
- Dana Jacobsen Oregon State University
- jacobsd@cs.orst.edu Computer Science
- .!hplabs!hp-pcd!orstcs!jacobsd
- Dana_Jacobsen@RPITSMTS.BITNET Power, Corruption, & Lies -- Bush '92
-