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:\>