How can I check if a Perl array contains a particular value?

This tutorial explains and Check If Element Exists in an Array of Perl With Code Examples.

The array contains a sequence of elements stored under a single value.

This tutorial shows you multiple examples in Perl to check if an element exists in an Array or not

How to check if an element exists in an array of Perl

There are multiple ways to check if an element exists

With linear Search, arrays are iterated sequentially and every element is matched with iterated element or not.

  • First, Use for loop to iterate an array, and $_ contains the temporary variable to hold iterated element,
  • Check current element matches the given element using the if conditional statement.
  • prints a string to the console.

Here is an example

@numbers = (1, 2, 3,4,5,6);
my $searchElement=1;

for (@numbers) {
    if ($_ eq $searchElement) {
       print "exists";
       last;
    }
}

Use smart match Operator

The smart match operator is an experimental feature and is introduced in the Perl 5.10 version.

Enable these features using use experimental 'smartwatch'; statement ~~ in Perl is a comparison and checks an element with iterated arrays.

use warnings;
use experimental 'smartwatch';


@numbers = (1, 2, 3,4,5,6);
my $searchElement=1;

if ($searchElement ~~ @numbers){
  print "Exists";

  } else {
    print "Not Exists";
}

In this example,

  • The ~~ operator checks if an element found in an array,
  • if condition is true, and element found, then if block executed, else block executed

Use the grep function

The grep() function iterates each element from an array based on regular expression and checks for equality with a given element.

  • grep function iterates each element of an array(@numbers)
  • { $_ == $searchElement } condition checks current element($_) with given element $searchElement
  • if the condition is a truthy, matching element found, grep returns a nonempty list and code inside if block executes, else falsely condition, and else block executed.

Here is an example

@numbers = (1, 2, 3,4,5,6);
my $searchElement=1;


if (grep { $_ == $searchElement } @numbers) {
    print "$searchElement exists in an Array\n";

} else {
    print "$searchElement does not exist in an Array\n";
}

The grep function takes more memory usage and should be avoided.

Use the CPAN module, any function

Another way is to install the CPAN module and use the any function.

This checks whether any given element matches with an array of elements and returns true or false

any(@array) eq 'element' checks whether an element exists in an array or not.

use this in conditional if to check boolean values.

Here is an example

use warnings;
use syntax 'junction';

@numbers = (1, 2, 3,4,5,6);
my $searchElement=1;

if ( any(@numbers) eq $searchElement )  {
  print "Exists";

  } else {
    print "Not Exists";
}

How do I check if an array contains a number?

To check if an array contains only scalar numbers, use the if conditional statement. use the grep function to check given element exists or not with the required regular expression. It returns true if a number scalar is found. else return false.

To summarize, I Demonstrated multiple ways to check given element contains an array. based on the Perl version, You can choose one approach to suit your needs