Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!udel!haven.umd.edu!mimsy!mojo!eng.umd.edu!ziegast From: ziegast@eng.umd.edu (Eric W. Ziegast) Newsgroups: comp.lang.perl Subject: Re: A question on associative arrays Message-ID: <1991May31.011755.4305@eng.umd.edu> Date: 31 May 91 01:17:55 GMT References: <1991May30.205555.14490@convex.com> Sender: news@eng.umd.edu (C-News) Reply-To: ziegast@eng.umd.edu (Eric W. Ziegast) Organization: University of Merryland, Engineering Computing Services Lines: 51 (Tom Christiansen) writes: > From the keyboard of (Przemek Klosowski): > How does one check if the associative array was assigned at all? > :My program runs a loop initializing an array, but each individual > :initialization is conditioned on something. On a rainy day all conditions > :could be false: is there a way to find out? The obvious $#ary doesn't work, > :since it refers to @ary. defined(%ary) is no good, since %ary might be > :pre-defined in a local(). Ideas, anyone? > > if (%a) { > > will actually work, but I don't think that Larry has ever documented > this. More portably, and more slowly would be > > if (@my_keys = keys %a) { Perhaps this should be documented. I gave it a few tests of wort-case scenarios... 1. Null assignment (an assignment nonetheless) $assoc{''}=''; print %assoc ? "true" : "false"; Results in true. 2. Defining the assoc as a local sub foo { local(%assoc); print %assoc ? "true" : "false"; } &foo Results in false. 3. A new dbm file dbmopen(%assoc,"newdbmfile",0600); print %assoc ? "true" : "false"; Results in false. I performed these tests with Perl 3.0 (41) on a SunOS 4.1 system. I believe ndbm was compiled into our perl. Just FYI. Someone more knowledgable might want to explain why/how this works. -- Eric Ziegast