Path: utzoo!attcan!uunet!wuarchive!sdd.hp.com!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Newsgroups: comp.lang.perl Subject: Re: associative arrays ? (repost) Message-ID: <10137@jpl-devvax.JPL.NASA.GOV> Date: 28 Oct 90 07:39:31 GMT References: Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 55 In article stef@zweig.sun (Stephane Payrard) writes: : Is length() supported when applied to an associative array? It's not even supported when applied to a normal array. : Why does it return a value equal to : number of keys + number of : values + 1 ? It doesn't, except by coincidence. : example: : % perl -e '%a=(1,2);print length %a;' : 3 The value of scalar %a is "num_buckets_occupied/num_buckets_allocated", or "0" for an empty array. For (1,2) it happens to be "1/4", which has a length of 3. I could conceivable keep track of the length of an associative array, and return that, but I don't. The main reason is dbm files. Where would I store the current length? What if a C program updates the dbm file? Or would I have to scan the whole dbm file whenever anyone said "if %a"? Shudder. : Why associative arrays are not interpreted in double quotes when : in the same context regular arrays? I feel it makes things inconsistent. : : example : % perl -e '%a=(1,2); print "%a\n";@a=(1,2);print "@a";' : %a : 1 2 What's the matter with inconsistency? After all, I'm basically irrational... There are two reasons for not making it consistent, actually. One is simply that it wasn't that way before, and to change it would break things gratuitously. (Especially since the % character occurs more frequently in ordinary text than @ does. (Not to mention printf formats. Do you really want all your printf formats to break when you decide to add a %s or *s variable to your program?)) The other reason is that people usually don't want that format for printing out associative arrays anyway. So why supply it? If I did add it, I'd probably make it print out "1=2". There are many other differences between associative arrays and normal arrays. It's only a coincidence that you can pass them both around as lists. I'd much rather add a pair() operator. Or some generalization thereof. print join("\n", pair('=', %ENV)); Larry