home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!mcsun!sunic!corax.udac.uu.se!irfu.se!jhd
- From: jhd@irfu.se (Jan D.)
- Subject: Update FAQ? Was: fast way to get number of elements in an assoc array?
- Message-ID: <1992Aug28.075021.21372@irfu.se>
- Date: Fri, 28 Aug 1992 07:50:21 GMT
- References: <1992Aug27.032106.20515@CS.ORST.EDU> <BtnIDn.390@news.cso.uiuc.edu>
- Organization: Swedish Institute of Space Physics, Uppsala, Sweden
- Keywords: associative array number count $#array $#
- Lines: 39
-
- In article <BtnIDn.390@news.cso.uiuc.edu> ejk@ux2.cso.uiuc.edu (Ed Kubaitis - CCSO) writes:
- >jacobsd@prism.cs.orst.edu (Dana Jacobsen) writes:
- > |
- > | Is there a function akin to $#array that works on associative arrays? I
- > |have an associative array with over 30,000 keys, and this is what I'm
- > |currently doing:
- > |
- > | @dummykey = keys(%records);
- > | print $#dummykey+1, " records loaded.\n";
- > | undef (@dummykey);
- > |
- > | The problem with this is that the key list takes up a significant chunk of
- > |memory, as well as being very slow. The alternative way of handling this is
- > |to keep a seperate variable that contains the number of elements in the
- > |array...
- >
- >From perl 4.0 patch #20:
- >
- > |Subject: scalar keys %array now counts keys for you
- >
- >So 'scalar(keys(%records))' or '$nkeys = keys(%records);' should work on
- >perl beyond 4.019. Don't know how fast it is on large arrays, though.
- >
-
- Hmm, looking at the source, it looks as if this is the way to go.
- keys(%records) in scalar context (or values(%records) for that matter)
- does NOT make a temporary array and then returns the length of the array (which
- I thought it did).
- It just counts the entries in %records. I can't imagine anything being faster.
-
- This feature isn't mentioned in the manpage though.
-
- So '$nkeys = keys(%records);' is the way to go.
-
- Maybe update FAQ on this (if not already done)?
-
- Jan D.
-
-
-