Perl search if variable contains word 1

Perl search if variable contains word

if (index($str, $substr) != -1) {
    print "$str contains $substr\n";
} 

Here is what the above code is Doing:
1. We’re using the index() function to check if the substring is present in the string.
2. The index() function returns the position of the substring in the string.
3. If the substring is not present in the string, the index() function returns -1.
4. We’re using the != operator to check if the index() function returns -1.
5. If the index() function returns -1, it means that the substring is not present in the string.
6. If the index() function returns a value other than -1, it means that the substring is present in the string.

Similar Posts