Newsgroups: comp.lang.perl Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!think.com!zaphod.mps.ohio-state.edu!wuarchive!uunet!convex!usenet From: Tom Christiansen Subject: Re: A question on associative arrays Message-ID: <1991May30.183802.3917@convex.com> Sender: usenet@convex.com (news access account) Nntp-Posting-Host: pixel.convex.com Reply-To: tchrist@convex.COM (Tom Christiansen) Organization: CONVEX Software Development, Richardson, TX References: <1991May30.134223.23225@Think.COM> Distribution: na Date: Thu, 30 May 1991 18:38:02 GMT Lines: 23 From the keyboard of sid@think.com (Sid Stuart): : :I am writing a Perl application in which I would like to store :into an associative array only if an entry for that key does :not exist. The problem is I may come up with duplicate keys and :I don't want to overwrite the contents of an entry. Is there a :cheap way to find out if a key is in use? By cheap I mean other :than using the functions "keys" or "each" to search through all :the keys of the array. Barring pathological cases (keys whose values are undef), you could do this: $ary{$key} = $val unless defined $ary{$key}; or perhaps $ary{$key} .= $val . ' '; --tom -- Tom Christiansen tchrist@convex.com convex!tchrist "So much mail, so little time."