[Code Sea Pick-up Shell Perl] find out whether a particular string exists in a string array

Source: Internet
Author: User


Preface


To retrieve whether a string exists in an array, the most basic idea should be to loop through the array and judge the value of each element and the given value to be equal.



(In the Java language you can also convert the array to list, in the list directly with the Contains method can be used)



Look at a simple code:





my @arr2 = qw(str1 str2 str3 str4);
foreach(@arr2)
{
	if($_ eq "str2")
    {
    	print "str2 exist in Array!\n";
    	last;
    }
}

This code looks very concise, and it's not hard to understand. But there is another way to do this in Perl, which requires a single line of code to achieve this effect. is to use the grep function







about grep


The full name of grep is the global Regular expression Print, which translates to an overall regex version.



If you have used a Linux command, you should be familiar with the word.



See a common Linux command


ps -ef | grep java
Ps-ef is to see all the processes





In addition to grep Java, the process of searching for a Java name in a system process.



That is, in Linux, grep can use regular expressions to search for text and print matching lines.






Comprehend by analogy, the grep function works similarly in Perl.





Perl grep function


In Perl, the grep function is called in two ways:



Mode 1. grep BLOCK LIST



Mode 2. grep EXPT, LIST



Block: Represents a code block, usually denoted by {};



EXPR represents an expression, usually a regular expression



List: Lists to match



The grep function matches each element of the list with block or expr, which iterates through the lists and temporarily sets the element to $_.



In the list context, GREP returns all the elements that match the hit, and the result is a list.



In a scalar context, GREP returns the number of elements that match the hit.



Compare the following code:





my @ arr3 = qw (str1 str2 str3 str4 str11);
my $ str = "str1";
my $ result = grep / ^ $ str /, @ arr3;
my @ result2 = grep / ^ $ str /, @ arr3;
print "grep result = $ result \ n"; # returns the number
print "grep [email protected] \ n"; # return element 

This matches the string element starting with str1 in the array.





(Hint in regular expression yes: ^--= start, $ = end)



So to match exactly to a string, use the following method:





My $result =grep/^ $str $/, @arr3;





In this respect, the topic given in the title has been reached.




[Code Sea Pick-up Shell Perl] find out whether a particular string exists in a string array


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.