home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / perl / 5529 < prev    next >
Encoding:
Internet Message Format  |  1992-08-26  |  1.7 KB

  1. Path: sparky!uunet!usc!cs.utexas.edu!uwm.edu!ogicse!orstcs!prism!jacobsd
  2. From: jacobsd@prism.cs.orst.edu (Dana Jacobsen)
  3. Newsgroups: comp.lang.perl
  4. Subject: fast way to get number of elements in an assoc array?
  5. Summary: is there a way to count elements in an assoc array better than keys?
  6. Keywords: associative array number count $#array $#
  7. Message-ID: <1992Aug27.032106.20515@CS.ORST.EDU>
  8. Date: 27 Aug 92 03:21:06 GMT
  9. Article-I.D.: CS.1992Aug27.032106.20515
  10. Sender: usenet@CS.ORST.EDU
  11. Organization: Oregon State University, Computer Science Dept
  12. Lines: 29
  13. Nntp-Posting-Host: prism.cs.orst.edu
  14.  
  15.  
  16.   Is there a function akin to $#array that works on associative arrays? I
  17. have an associative array with over 30,000 keys, and this is what I'm 
  18. currently doing:
  19.  
  20.     @dummykey = keys(%records);
  21.     print $#dummykey+1, " records loaded.\n";
  22.     undef (@dummykey);
  23.  
  24.   The problem with this is that the key list takes up a significant chunk of
  25. memory, as well as being very slow.  The alternative way of handling this is
  26. to keep a seperate variable that contains the number of elements in the 
  27. array, something like:
  28.  
  29.   $records{$name} || $numberofrecords++;
  30.   $records{$name} .= $; . $citekey;
  31.  
  32. However, this seems unaesthetic, as well as raising the possibility (if records
  33. got added elsewhere in the code also) of having an incorrect count.
  34.  
  35.   Any ideas?
  36. --
  37. Dana Jacobsen                "After learning Perl, I can't wait for Swine!"
  38. jacobsd@cs.orst.edu
  39. jacobsd@solar.cor2.epa.gov              Power, Corruption, & Lies -- Bush '92
  40. Dana Jacobsen                       Oregon State University
  41. jacobsd@cs.orst.edu                   Computer Science
  42. .!hplabs!hp-pcd!orstcs!jacobsd
  43. Dana_Jacobsen@RPITSMTS.BITNET           Power, Corruption, & Lies -- Bush '92
  44.