Path: utzoo!utgpu!watserv1!watmath!att!att!linac!pacific.mps.ohio-state.edu!zaphod.mps.ohio-state.edu!sdd.hp.com!ucsd!ucbvax!ucdavis!csusac!utgard!chris From: chris@utgard.uucp (Chris Anderson) Newsgroups: comp.lang.perl Subject: Re: The best way to search an array ? Keywords: detab Message-ID: <1990Nov28.160822.21351@utgard.uucp> Date: 28 Nov 90 16:08:22 GMT References: <443@stephsf.stephsf.com> Followup-To: comp.lang.perl Distribution: na Organization: QMA, Inc., Rancho Cordova, California Lines: 40 In article <443@stephsf.stephsf.com> wengland@stephsf.stephsf.com (Bill England) writes: > What is the best method for discovering if a string is in > a given set of strings? For example say you want to verify > that the string 'WA' is a valid two char state abreviation? > > I can think of at least four methods ... [methods deleted] > 2 Build an associative array %state so that you have; > $state{'AK'}="Alaska"; > $state{'AL'}="Alabama"; > $state{'AR'}="Arkansas"; > $state{'AZ'}="Arizona"; > $state{'CA'}="California"; > ... > Then test for the existance of $state{$prove_state}. > ( Has disadvantage of extra bagage if you don't need the > states full name. ) Naah. You don't need the extra baggage: foreach $one (@all_the_states) { $states{$one}++; } or undef %states; @states{@all_the_states} = (); Either way, you don't have the baggage and you can find the name easily and quickly. Associative arrays are great! Chris -- +---------------------------------------------------------------+ | Chris Anderson, QMA, Inc. utgard!chris@csusac.csus.edu | | My employer doesn't listen to me... why should you? | +---------------------------------------------------------------+