On Tue, 11 Feb 2003 17:31:42 +0000, Felix Geerinckx wrote: > on di, 11 feb 2003 17:22:20 GMT, Dan Muey wrote: > >> if(exists $hash{$key}) { ... } >> >> is there a way to do that with arrays? >> IE >> if there is an array element that is 'bob' then do this :: >> without having to do a foreach on the array > > See > perldoc -q "array contains" > > in the Frequently Asked Questions.
perldoc -q "array contains" results to no documentation at my perl (5.8.0). Is there a new faq ? perldoc -q "element is contained in a list or array" works at my computer. But BTW, the faq is not correct (I'll submit a change proposition) Please do not use ($is_there) = grep $_ eq $whatever, @array; or worse yet ($is_there) = grep /$whatever/, @array; These are slow (checks every element even if the first matches), inefficient (same reason), and potentially buggy (what if there are regex characters in $whatever?). If you're only testing once, then use: Allthough, these are O(n) solutions, for normal sized lists, they are very quick! Reason is that grep is a builtin, compiled to one opcode, where the workarounds consists of many opcodes reducing the running time on real pcs. That's why grep is for normal (little) sizes quicker than all non builtin functions! I also feel that it is easier to read, unless someone really writes an exist function :-). Greetings, Janek PS: There had been a long discussion about it in the summer last year on compl.lang.perl.misc. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]