Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!newstop!sundc!texsun!convex!convex.COM From: tchrist@convex.COM (Tom Christiansen) Newsgroups: comp.lang.perl Subject: Re: The best way to search an array ? Keywords: detab Message-ID: <109494@convex.convex.com> Date: 29 Nov 90 16:08:20 GMT References: <443@stephsf.stephsf.com> <1990Nov28.160822.21351@utgard.uucp> Sender: news@convex.com Reply-To: tchrist@convex.COM (Tom Christiansen) Distribution: na Organization: CONVEX Software Development, Richardson, TX Lines: 58 In article <1990Nov28.160822.21351@utgard.uucp> chris@utgard.uucp (Chris Anderson) writes: > 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! Yes, they're great, but the recent patches have made the quoted usage of questionable utility. This I find highly unfortunate, since it's so very useful. The problem is Larry made it so you can have a key defined, but whose value is undefined. This means strange things like this happen: @colors = ( 'red', 'blue', 'green' ); @seen{@colors} = (); print "defined\n" if defined $seen{'blue'}; print "undefined\n" unless defined $seen{'blue'}; That now prints undefined, although it used to print defined. You can still say for (@seen) { $colors{$_}++; } with an explicit loop, but I really like the @foo{@bar} = () usage. I can't figure out WHY you want entries in an associative arrays with undefined values. Does doing that really make sense?? Larry addressed this in <10334@jpl-devvax.JPL.NASA.GOV> on 12 Nov 90 19:22:14 GMT, in a message entitled "Re: defined() on an undef array element created via slice." After granting that correct interpretation of this is a judgment call, he pointed out that it's nice to distinguish between @foo{"bar"} = (); and @foo{"bar"} = (''); (I still get nervous with that usage because despite the parens, novices will think it a scalar assignment as there's only one element in the slice.) But what really bothers me is that @seen{@colors} = (''); makes red defined and blue not, but they're both valid keys, and this isn't what I call following the principle the least surprise. @seen{@colors} = (1..$#colors); is a hack that will get you by, but I still prefer the prior behavior. Maybe I just got too used to it. --tom