Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: without looping, check if $var is found in @list

by duckyd (Hermit)
on Dec 14, 2006 at 02:51 UTC ( [id://589727] : note . print w/replies, xml ) Need Help??


in reply to without looping, check if $var is found in @list

If you need to do this sort of operation (is $foo in @list) often, you should probably be using a hash rather than a list, even if your values are undef.
#!/usr/local/bin/perl use strict; use warnings; my @list = qw(frog turtle tadpole); my $var = "turtle"; my %check_list; @check_list{ @list } = (); if( exists $check_list{ $var } ){ print "found $var!\n"; }
Obviously you could get rid of @list entirely above.