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

  1. Path: sparky!uunet!gatech!rutgers!network.ucsd.edu!news!netlabs!lwall
  2. From: lwall@netlabs.com (Larry Wall)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: fast way to get number of elements in an assoc array?
  5. Message-ID: <1992Aug31.173856.2516@netlabs.com>
  6. Date: 31 Aug 92 17:38:56 GMT
  7. References: <1992Aug27.152838.17427@news.eng.convex.com> <1992Aug29.011345.24106@lsil.com>
  8. Sender: news@netlabs.com
  9. Organization: NetLabs, Inc.
  10. Lines: 22
  11. Nntp-Posting-Host: scalpel.netlabs.com
  12.  
  13. In article <1992Aug29.011345.24106@lsil.com> aspin@lsil.com writes:
  14. : I wrote a program to try various ways of doing this,
  15. : including the one above, and get several different results.
  16. : Can anyone explain this to me?
  17.  
  18. : while (<IN>) {
  19. :     $words{$_} = $nlines++;
  20. : }
  21.  
  22. Note that this sets the first entry in %words to have a value of 0.  After
  23. the loop this turns out to be the 14th entry (in iterator order).
  24.  
  25. : &try('$n = keys(%words)');
  26. : &try('$n = 0; while (each %words) { $n++; }');
  27.  
  28. Note that this evaluates each() in a scalar context, so it returns just
  29. the value.  The 14th time it does this, it gets a 0, so your while
  30. loop exits before the iterator is done.  Next time you'll start from
  31. the 15th entry, and since there are 84 entries left on the iterator,
  32. that's what you'll get for a count.
  33.  
  34. Larry
  35.