在Perl中,我可以使用它们之间的空格连接多个字符串,如下所示:
my $long_string = $one_string . " " . $another_string . " " . $yet_another_string . " " .
$and_another_string . " " $the_lastr_string
Run Code Online (Sandbox Code Playgroud)
但是,输入这个有点麻烦.
是否有内置功能可以使这项任务更容易?
例如:
concatenate_with_spaces($one_string, $another_string, $yet_another_string, ...)
Run Code Online (Sandbox Code Playgroud)
Zai*_*aid 13
你想要join
:
my $x = 'X';
my @vars = ( 1, 'then', 'some' );
my $long_string = join ' ', $x, 2, @vars; # "X 2 1 then some"
Run Code Online (Sandbox Code Playgroud)
Zaid给出了惯用的解决方案,使用join
.但是,还有更多方法可以做到这一点.
my @vars = ($one, $two, $three);
my $str1 = "@vars"; # Using array interpolation
my $str2 = "$one $two $three"; # interpolating scalars directly
Run Code Online (Sandbox Code Playgroud)
插值数组使用预定义变量$"
(列表分隔符),默认情况下设置为空格.在插入变量时,您不需要使用.
将空格连接到字符串,它们可以直接在双引号字符串中使用.