home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!gatech!rutgers!network.ucsd.edu!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: <1992Aug31.173856.2516@netlabs.com>
- Date: 31 Aug 92 17:38:56 GMT
- References: <1992Aug27.152838.17427@news.eng.convex.com> <1992Aug29.011345.24106@lsil.com>
- Sender: news@netlabs.com
- Organization: NetLabs, Inc.
- Lines: 22
- Nntp-Posting-Host: scalpel.netlabs.com
-
- In article <1992Aug29.011345.24106@lsil.com> aspin@lsil.com writes:
- : I wrote a program to try various ways of doing this,
- : including the one above, and get several different results.
- : Can anyone explain this to me?
-
- : while (<IN>) {
- : $words{$_} = $nlines++;
- : }
-
- Note that this sets the first entry in %words to have a value of 0. After
- the loop this turns out to be the 14th entry (in iterator order).
-
- : &try('$n = keys(%words)');
- : &try('$n = 0; while (each %words) { $n++; }');
-
- Note that this evaluates each() in a scalar context, so it returns just
- the value. The 14th time it does this, it gets a 0, so your while
- loop exits before the iterator is done. Next time you'll start from
- the 15th entry, and since there are 84 entries left on the iterator,
- that's what you'll get for a count.
-
- Larry
-