使用或不使用双引号时打印数组变量
当我学习打印数组变量时,我发现使用双引号时会插入空格。片段代码如下。你能告诉我为什么吗?
#!/usr/bin/perl -w
use strict;
use warnings;
my @str_array = ("Perl","array","tutorial");
my @int_array = (5,7,9,10);
print @str_array;
print "\n";
# added the double quotes
print "@str_array";
print "\n";
print @int_array;
print "\n";
# added the double quotes
print "@int_array";
输出:
Perlarraytutorial
Perl array tutorial
57910
5 7 9 10
When I learning to print array variables, I found the white space inserted when double quoter used. Snippet code as below. Could you please tell me why?
#!/usr/bin/perl -w
use strict;
use warnings;
my @str_array = ("Perl","array","tutorial");
my @int_array = (5,7,9,10);
print @str_array;
print "\n";
# added the double quotes
print "@str_array";
print "\n";
print @int_array;
print "\n";
# added the double quotes
print "@int_array";
Output:
Perlarraytutorial
Perl array tutorial
57910
5 7 9 10
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是回答您问题的 perlfaq:
为什么打印行数组时会出现奇怪的空格?
Here is the perlfaq that answers your question:
Why do I get weird spaces when I print an array of lines?
当在字符串内使用变量时,将使用字符串插值。字符串插值意味着变量被格式化为字符串。当数组变量格式化为字符串时,元素之间会留有空格。这些空格实际上是由变量 $" 确定的。该变量的默认值为空格。有关详细信息,请参阅 特殊变量的 Perl 文档。
When a variable is used inside a string, string interpolation is used. String interpolation means, among other things, the variable is formatted to become a string. When an array variable is formatted to become a string it is given spaces between the elements. These spaces are actually determined by the variable $". The default value for this variable is a space. For more information see the perl documentation for special variables.