home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!gatech!destroyer!sol.ctr.columbia.edu!usc!news!netlabs!lwall
- From: lwall@netlabs.com (Larry Wall)
- Newsgroups: comp.lang.perl
- Subject: Re: fast way to get number of elements in an assoc array?
- Message-ID: <1992Aug28.193915.10309@netlabs.com>
- Date: 28 Aug 92 19:39:15 GMT
- References: <1992Aug27.032106.20515@CS.ORST.EDU> <ASHERMAN.92Aug27111710@laser.fmrco.com>
- Sender: news@netlabs.com
- Organization: NetLabs, Inc.
- Lines: 35
- Nntp-Posting-Host: scalpel.netlabs.com
-
- In article <ASHERMAN.92Aug27111710@laser.fmrco.com> asherman@fmrco.COM writes:
- : Remember, arrays in a scalar context USUALLY return the number of
- : items that they contain.
-
- Close. I'd say something like, operators that return lists in array
- context, when evaluated in a scalar context, USUALLY return the number
- of items they would have returned in the array context.
-
- This is often the most useful scalar behavior when you don't know the
- number of elements. But for some operators that return a *known*
- number of elements (or none on failure), it's more useful to return a
- scalar value derived from one or more of the potential list values.
- (As a familiar example, the C comma operator is required to return the
- last value. You might not think of the comma operator as having a
- known number of return values, and it doesn't in general, but it does
- in *specific* (usually). It's not often that you type a list of items
- into a program only to find out how many there are. You can see that
- just by looking at it.)
-
- If you like general rules, here's another one: If the array context
- would have elicited a null list, the scalar context will elicit a
- false value. So ideally there should rarely be any logical (Boolean)
- difference between
-
- if ($foo = some_operator) {
-
- and
-
- if (($bar) = some_operator) {
-
- But there's no guarantee that $foo and $bar get the same value. It all
- depends on what some_operator thinks is most useful in a scalar context.
- As in English, every Perl verb has its own valence. But there are patterns.
-
- Larry
-