Perl:split function Usage

Source: Internet
Author: User

In this paper, we focus on the use of the Perl split function, a very useful function in Perl is the Perl split function-splitting the string and putting the segmented result into the array. This Perl split function uses the regular expression (RE) and works on the $_ variable if it is not specific.

Perl Split function

A very useful function in Perl is the Perl split function-splitting the string and putting the segmented result into the array. This Perl split function uses the regular expression (RE) and works on the $_ variable if it is not specific.

The Perl split function can be used like this:

    1. $info="caine:michael:actor:14,leafydrive";
    2. @personal=split (/:/, $info);

The result is: @personal = ("Caine", "Michael", "Actor", "14,leafydrive");

If we have stored the information in the $_ variable, we can do this:

@personal =split (/:/);

If each field is delimited by any number of colons, it can be split with the RE code:

    1. $_="Capes:geoff::shotputter:::bigavenue";
    2. @personal=split (/:+/);

The result is: @personal = ("Capes", "Geoff", "Shotputter", "bigavenue");

But the following code:

    1. $_="Capes:geoff::shotputter:::bigavenue";
    2. @personal=split (/:/);

The result is: @personal = ("Capes", "Geoff", "" "," Shotputter "," "," "", "bigavenue");

In this Perl split function, words can be split into characters, sentences can be divided into words, and paragraphs can be divided into sentences:

    1. @chars=split (//, $word);
    2. @words=split (//, $sentence);
    3. @sentences=split (/\./, $paragraph);

In the first sentence, an empty string is matched between each character, so the @chars array is an array of characters. >>

The section between is the regular expression (or the separation rule) that the split uses
\s is a wildcard character that represents a space
+ represents repeating one or more times.
So, \s+ represents one or more spaces.
Split (/\s+/, $line) indicates that the string is $line, separated by a space.
For example, $line = "Hello friends Welcome to my blog 61dh.com";
Split (/\s+/, $line) is obtained after:
Hello Friend Welcome to visit my blog 61dh.com

Perl:split function Usage

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.