In Perl, an array is a special type of variable that is used to store a list of elements.
There are multiple ways to check if an array contains a particular value. In this shot, we will use grep()
.
grep()
methodThe method uses
grep( /^$value$/, @nums )
It takes two parameters value
and array
.
value
: Provides value to search for.array
: Provides array to search in.It returns a boolean value.
In the following example, we will check if the given value 90
is present in the given array nums
or if it's using grep()
.
#provide nums array@nums = (10, 20, 30, 40 , 50);#provide value to searchmy $value = 90;#check if array contains given valueif ( grep( /^$value$/, @nums ) ) {print "The given value contains in the array";}else{print "The given value does not contain in the array";}
In the code snippet above:
nums
.grep()
. We will provide the output statement according to the return value from grep()
.