Testing if value is in an array

I am having trouble with a simple problem.  How can I check/test if a value is in an array?

@attendees = ("kim","bob","jim","john");
$value = "john"


@numbers= (3,5,6,8);
$check_value = 5;


Is there a function that easily tells me if $value is in @attendees or $check_value is in @numbers?


thanks!
cs76737Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TintinCommented:
Sounds like you should be using a hash instead, then you can do

print "exists" if $attendees{'john'};
print "exists" if $numbers{'5'};
0
cs76737Author Commented:
A hash isn't how this data is structured.  I will have a $input, and I just want to see if $input is one of the values in a list @test = (2,5,6,7,4,3,);

If I had to create a hash, it would be more difficult right?  Wouldn't I have to code:
$hash{'2'} = " "
$hash{'4'} = " "
....

print "exists" if $hash{'5'}

Or is there an easier way to create this hash?
0
TintinCommented:
You can create a hash like

my %attendees = (
   kim  => 1,
   bob => 1,
   jim   => 1,
   john => 1
);
0
TintinCommented:
The entry from the Perl FAQ is also useful to read:

     How can I tell whether a certain element is contained in a list or array?

       (portions of this answer contributed by Anno Siegel)

       Hearing the word "in" is an indication that you probably should have used a hash, not a list or array, to store your data.  Hashes are designed to answer this
       question quickly and efficiently.  Arrays aren't.

       That being said, there are several ways to approach this.  If you are going to make this query many times over arbitrary string values, the fastest way is prob-
       ably to invert the original array and maintain a hash whose keys are the first array's values.

           @blues = qw/azure cerulean teal turquoise lapis-lazuli/;
           %is_blue = ();
           for (@blues) { $is_blue{$_} = 1 }

       Now you can check whether $is_blue{$some_color}.  It might have been a good idea to keep the blues all in a hash in the first place.

       If the values are all small integers, you could use a simple indexed array.  This kind of an array will take up less space:

           @primes = (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31);
           @is_tiny_prime = ();
           for (@primes) { $is_tiny_prime[$_] = 1 }
           # or simply  @istiny_prime[@primes] = (1) x @primes;

       Now you check whether $is_tiny_prime[$some_number].

       If the values in question are integers instead of strings, you can save quite a lot of space by using bit strings instead:

           @articles = ( 1..10, 150..2000, 2017 );
           undef $read;
           for (@articles) { vec($read,$_,1) = 1 }

       Now check whether "vec($read,$n,1)" is true for some $n.

       These methods guarantee fast individual tests but require a re-organization of the original list or array.  They only pay off if you have to test multiple val-
       ues against the same array.

       If you are testing only once, the standard module List::Util exports the function "first" for this purpose.  It works by stopping once it finds the element.
       It's written in C for speed, and its Perl equivalant looks like this subroutine:

               sub first (&@) {
                       my $code = shift;
                       foreach (@_) {
                               return $_ if &{$code}();
                       }
                       undef;
               }

       If speed is of little concern, the common idiom uses grep in scalar context (which returns the number of items that passed its condition) to traverse the entire
       list. This does have the benefit of telling you how many matches it found, though.

               my $is_there = grep $_ eq $whatever, @array;

       If you want to actually extract the matching elements, simply use grep in list context.

               my @matches = grep $_ eq $whatever, @array;

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cs76737Author Commented:
thank you.  very helpful post.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.