home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / perl / 5548 < prev    next >
Encoding:
Text File  |  1992-08-27  |  1.5 KB  |  38 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!cs.utexas.edu!usc!sdd.hp.com!ux1.cso.uiuc.edu!news.cso.uiuc.edu!ux2.cso.uiuc.edu!ejk
  3. From: ejk@ux2.cso.uiuc.edu (Ed Kubaitis - CCSO)
  4. Subject: Re: fast way to get number of elements in an assoc array?
  5. References:  <1992Aug27.032106.20515@CS.ORST.EDU>
  6. Message-ID: <BtnIDn.390@news.cso.uiuc.edu>
  7. Sender: usenet@news.cso.uiuc.edu (Net Noise owner)
  8. Organization: University of Illinois - Urbana
  9. Date: Thu, 27 Aug 1992 16:41:42 GMT
  10. Keywords: associative array number count $#array $#
  11. Lines: 25
  12.  
  13. jacobsd@prism.cs.orst.edu (Dana Jacobsen) writes:
  14.   |
  15.   |  Is there a function akin to $#array that works on associative arrays? I
  16.   |have an associative array with over 30,000 keys, and this is what I'm 
  17.   |currently doing:
  18.   |
  19.   |    @dummykey = keys(%records);
  20.   |    print $#dummykey+1, " records loaded.\n";
  21.   |    undef (@dummykey);
  22.   |
  23.   |  The problem with this is that the key list takes up a significant chunk of
  24.   |memory, as well as being very slow.  The alternative way of handling this is
  25.   |to keep a seperate variable that contains the number of elements in the 
  26.   |array...
  27.  
  28. From perl 4.0 patch #20: 
  29.  
  30.   |Subject: scalar keys %array now counts keys for you
  31.  
  32. So 'scalar(keys(%records))' or '$nkeys = keys(%records);' should work on 
  33. perl beyond 4.019. Don't know how fast it is on large arrays, though.
  34.  
  35. ----------------------------------
  36. Ed Kubaitis (ejk@ux2.cso.uiuc.edu)
  37. Computing & Communications Services Office - University of Illinois, Urbana
  38.