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

  1. Path: sparky!uunet!gatech!destroyer!sol.ctr.columbia.edu!usc!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: <1992Aug28.193915.10309@netlabs.com>
  6. Date: 28 Aug 92 19:39:15 GMT
  7. References: <1992Aug27.032106.20515@CS.ORST.EDU> <ASHERMAN.92Aug27111710@laser.fmrco.com>
  8. Sender: news@netlabs.com
  9. Organization: NetLabs, Inc.
  10. Lines: 35
  11. Nntp-Posting-Host: scalpel.netlabs.com
  12.  
  13. In article <ASHERMAN.92Aug27111710@laser.fmrco.com> asherman@fmrco.COM writes:
  14. : Remember, arrays in a scalar context USUALLY return the number of
  15. : items that they contain. 
  16.  
  17. Close.  I'd say something like, operators that return lists in array
  18. context, when evaluated in a scalar context, USUALLY return the number
  19. of items they would have returned in the array context.
  20.  
  21. This is often the most useful scalar behavior when you don't know the
  22. number of elements.  But for some operators that return a *known*
  23. number of elements (or none on failure), it's more useful to return a
  24. scalar value derived from one or more of the potential list values.
  25. (As a familiar example, the C comma operator is required to return the
  26. last value.  You might not think of the comma operator as having a
  27. known number of return values, and it doesn't in general, but it does
  28. in *specific* (usually).  It's not often that you type a list of items
  29. into a program only to find out how many there are.  You can see that
  30. just by looking at it.)
  31.  
  32. If you like general rules, here's another one:  If the array context
  33. would have elicited a null list, the scalar context will elicit a
  34. false value.  So ideally there should rarely be any logical (Boolean)
  35. difference between
  36.  
  37.     if ($foo = some_operator) {
  38.  
  39. and
  40.  
  41.     if (($bar) = some_operator) {
  42.  
  43. But there's no guarantee that $foo and $bar get the same value.  It all
  44. depends on what some_operator thinks is most useful in a scalar context.
  45. As in English, every Perl verb has its own valence.  But there are patterns.
  46.  
  47. Larry
  48.