Perl QW Solutions with space-delimited problems _ Application Tips

Source: Internet
Author: User

When you create an array in Perl, you can use the QW.

But there is a problem, if you want to create an array of 20 names, and everyone's name is this form of "Join Smith" "Harry Potter" which means that each name contains both a surname and a name. At this point, the QW will not work. Because QW is a space as a separator.

Here are some alternative solutions for your reference.

Programme I:

Use the original scheme, which is double quotes, to create the array

Copy Code code as follows:

@names = ("Join Smith", "Harry Potter");
Print @names [0];

The results are as follows:
f:\>perl\b.pl
Join Smith
F:\>

Programme II:

We can do a simple workaround, QW can only be separated by a space, then we put the space between the names of people with other characters to replace.
@names =qw/join_smith harry_potter/;
#然后当我们输出的时候我们再把中间的连接符进行替换

Copy Code code as follows:

@names [0]=~s/_//g;
Print @names [0];

The results are as follows;
f:\>perl\b.pl
Join Smith
F:\>

Programme III:

Created using the Split function.

Copy Code code as follows:

#首先我们定义一个变量
$names = "Join smith,harry Potter",

#这里利用split函数, here split//two diagonal lines is where you want to split, in this case the comma is the dividing line
My @names =split/,/, $names;
Rint @names [0];

The results are 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.