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