Tutorial on Split operator _ PHP

Source: Internet
Author: User
Split operator. Split splits a string based on the given mode. for strings that use tabs, colons, blank spaces, or any symbol to separate different fields, this operator is used to extract and Split fields. it splits strings according to the given mode. for strings that use tabs, colons, white spaces, or any symbol to separate different fields, it is quite convenient to use this operator to separate and extract fields. As long as you can write the delimiter into a pattern (usually a simple regular expression), you can use Split to Split the data. Its usage is as follows:

My @ fields = split/separator/, $ string;

Here, the Split operator scans the specified string in Split mode and returns a list of fields (that is, substrings. During this period, as long as the pattern matches successfully somewhere, it is the end of the current field and the beginning of the next field. Therefore, content in any matching mode will not appear in the returned field. The following is a typical Split mode with a colon as the separator:

My @ fields = split/:/, "abc: def: g: h"; # get ("abc", "def", "g", "h ")

If two delimiters are connected together, an empty field is generated:

My @ fields = split/:/, "abc: def: g: h"; # obtain ("abc", "def", "", "g ", "h ")

Here is a rule, which is odd at first glance, but rarely causes problems: Split retains the blank fields at the beginning, but removes the empty fields at the end. For example:

My @ fields = split/:/, ": a: B: c:"; # obtain ("", "", "B", "c ")

It is also common to use the/\ s +/mode of Split to separate characters with blank spaces. This mode treats all consecutive blank spaces as a single space and splits the data accordingly:

My $ some_input = "This is a \ t test. \ n ";

My @ args = split/\ s +/, $ some_input; # obtain ("This", "is", "a", "test .")

By default, Split separates strings in $ _ with blank spaces:

My @ fields = split; # equivalent to split/\ s +/, $ _;

This is almost equal to/\ s +/, but it will omit the Null fields starting. Therefore, even if the row starts with a blank line, you will not see an empty field at the beginning of the returned list. If you want to break down strings separated by spaces in this way, you can use a space as the mode: split '', $ other_string uses a space as the mode, which is a special usage of split.

In general, the mode used in Split is as simple as previously seen. However, if you use a more complex mode, avoid using parentheses in the mode, because this starts the so-called "delimiter retention mode (for details, see the Perlfunc document ). If you need to use group matching in the mode, use non-capturing parentheses in Split (? :) To avoid exceptions.

Further deepen the convenience of fields extracted by Split decomposition. The following is a piece of code that does not use the Split operator to break down and extract fields in my actual work (the code that uses the Split operator will be provided later). let's take a look at its strength:

Task: extract the user name and user home directory information from the passwd file;

Let's take a look at the record format in the passwd file (-1 part excerpt ):

Root: x: 0: 0: root:/bin/bash

Bin: x: 1: 1: bin:/bin/sh

......

We can see that each field is separated by a colon (:). taking the first record from left to right as an example, we need to extract the root (user name) before the first colon) and/root (user main directory) before the sixth colon ).

[Php]

# Code 1.1 the Field Code is not extracted using the Split operator;

#! /Usr/bin/perl-w

Use strict;

Open (FH, '/etc/passwd') or die "Can't open file: $! ";

While ( ){

My ($ Pos, $ endPos, $ length, $ Name, $ Dir );

#############

# Retrieve the user name

#############

$ Length = index ($ _,":");

$ Name = substr ($ _, 0, $ length );

#####################

# Retrieve the location of the user's HOME directory

#####################

$ EndPos = rindex ($ _,":");

# $ EndPos-1 skip current position (colon)

$ Pos = rindex ($ _, ":", $ endPos-1 );

# $ Pos + 1 skip the current position (colon)

# The search direction is from left to right. So + 1

$ Pos + = 1;

$ Length = $ endPos-$ Pos;

$ Dir = substr ($ _, $ Pos, $ length );

Print "$ Name \ t $ Dir \ n ";

}

Close (FH );

After the program runs, the output is as follows (-2 ):

Root/root

Bin/bin

......

Now let's analyze the algorithm of this code. to extract the user name, you just need to find the substring returned by the first Colon through the substr ($ _, 0, $ length) function, that is, the user name you need. The complex part of the algorithm is to extract the user's main directory. it can be seen through-1 that the passwd file itself has a fixed format and records are directed from the back to the back (from the right to the left) the/root after the penultimate colon is the user's home directory information.

Extract the algorithm idea of the user's main directory:

1. skip the last field of the record;

2. locate the start position of the last and second fields;

3. the start position (colon) of the last-to-last field minus the start position (/) of the second-to-last field character. The result is the character length in the user's main directory field;

4. substr ($ _, $ Pos, $ length); returns the user's home directory information;

5. complete.

(Figure 1-3 user directory extraction algorithm)

To sum up, we can use the Perl string processing function to locate and extract field information to complete our tasks. it is foreseeable that when we want to extract multiple unconnected fields, the steps will be more complicated and the code will be longer, it is more prone to errors. if the location of each field is changed, you will have to redesign your algorithm.

Now let's look at the example of splitting and extracting fields using the Split operator:

[Php]

# Code 1.2 use the Split operator to extract the Field Code;

#! /Usr/bin/perl-w

Use strict;

Open (FH, '/etc/passwd') or die "Can't open file: $! ";

While ( ){

###########

# Retrieving user information

###########

My ($ Name, $ Dir) = (split/:/, $ _) [0, 5];

Print "$ Name \ t $ Dir \ n ";

}

Close (FH );

.

...

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.