使用或不使用双引号时打印数组变量

发布于 2024-08-28 19:41:33 字数 452 浏览 9 评论 0原文

当我学习打印数组变量时,我发现使用双引号时会插入空格。片段代码如下。你能告诉我为什么吗?

#!/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄荷梦 2024-09-04 19:41:33

Here is the perlfaq that answers your question:

Why do I get weird spaces when I print an array of lines?

娇柔作态 2024-09-04 19:41:33

当在字符串内使用变量时,将使用字符串插值。字符串插值意味着变量被格式化为字符串。当数组变量格式化为字符串时,元素之间会留有空格。这些空格实际上是由变量 $" 确定的。该变量的默认值为空格。有关详细信息,请参阅 特殊变量的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文