Solutions to the problem of using space as the separator for perl qw

Source: Internet
Author: User

When creating an array in perl, you can use qw.

However, if you want to create an array consisting of 20 people, and the name of each person is in this form of "Join smith" "Harry Potter", that is, each name contains both a surname and a name. In this case, qw does not work. Because qw uses space as the separator.

The following provides some alternative solutions for your reference.

Solution 1:

Use the original solution, that is, double quotation marks, to create an array.

Copy codeThe Code is as follows: @ names = ("Join smith", "Harry Potter ");
Print @ names [0];

The result is as follows:
F: \> perl \ B. pl
Join smith
F: \>

Solution 2:

We can make a simple change. qw can only use space as the separator, so we will replace the space in the middle of the person's name with other characters.
@ Names = qw/Join_smith Harry_Potter /;
# Then, when we output the data, we will replace the intermediate connector.

Copy codeThe Code is as follows: @ names [0] = ~ S/_ // g;
Print @ names [0];

The result is as follows;
F: \> perl \ B. pl
Join smith
F: \>

Solution 3:

Create with the split function.

Copy codeThe Code is as follows: # first, we define a variable.
$ Names = "Join smith, Harry potter ",

# Here we use the split function. Here, the split // two diagonal lines are the place you want to split. In this example, we use commas as the Division boundary.
My @ names = split/,/, $ names;
Rint @ names [0];

The result is as follows:
F: \> perl \ B. pl
Join smith
F: \>

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.