home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!wupost!usc!news!lsi!mhost!up41!aspin
- From: aspin@lsil.com (David Aspinwall 7842)
- Newsgroups: comp.lang.perl
- Subject: Re: fast way to get number of elements in an assoc array?
- Message-ID: <1992Aug29.011345.24106@lsil.com>
- Date: 29 Aug 92 01:13:45 GMT
- References: <1992Aug27.152838.17427@news.eng.convex.com>
- Sender: news@lsil.com (news caster)
- Reply-To: aspin@lsil.com
- Organization: LSI Logic Corporation
- Lines: 52
- Nntp-Posting-Host: up41
- X-Newsreader: Tin 1.1 PL5
-
- Tom Christiansen (tchrist@convex.COM) wrote:
- : Um, that doesn't tell you how many keys are in the array, just
- : how full the hashtable is. You have to do something more like
- :
- : $count++ while each %array;
-
- 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?
- Here's the program:
-
-
- #!/usr/up/sun4bin/perl
-
- open(IN, "head -100 /usr/dict/words|");
- while (<IN>) {
- $words{$_} = $nlines++;
- }
- close(IN);
- print "read $nlines lines\n";
-
- &try('$n = keys(%words)');
- &try('$n = 0; while (each %words) { $n++; }');
- &try('$n = 0; $n++ while each %words;');
- &try('$n = 0; $n++ while (each %words);');
- &try('$n = 0; $n++ while (($w,$v) = (each %words));');
- &try('$n = 0; while (($w,$v) = each %words) { $n++; }');
- &try('$n = 0; while (($w,$v) = (each %words)) { $n++; }');
-
-
- sub try {
- local ($cmd) = @_;
- local ($n);
- eval($cmd);
- print "i get $n from\t$cmd\n";
- }
-
-
-
- And here is the output:
-
- read 100 lines
- i get 100 from $n = keys(%words)
- i get 13 from $n = 0; while (each %words) { $n++; }
- i get 86 from $n = 0; $n++ while each %words;
- i get 13 from $n = 0; $n++ while (each %words);
- i get 86 from $n = 0; $n++ while (($w,$v) = (each %words));
- i get 100 from $n = 0; while (($w,$v) = each %words) { $n++; }
- i get 100 from $n = 0; while (($w,$v) = (each %words)) { $n++; }
-
- --
- -- David Aspinwall aspin@lsil.com 408-433-7842
-