【问题标题】:Print array variables when using or not using double quotes使用或不使用双引号时打印数组变量
【发布时间】:2011-02-07 07:54:15
【问题描述】:

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

#!/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

【问题讨论】:

    标签: perl arrays printing double-quotes


    【解决方案1】:

    这是回答您问题的 perlfaq:

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

    【讨论】:

      【解决方案2】:

      在字符串中使用变量时,使用字符串插值。除其他外,字符串插值意味着将变量格式化为字符串。当一个数组变量被格式化为一个字符串时,它在元素之间被赋予了空格。这些空格实际上是由变量 $" 决定的。这个变量的默认值是一个空格。更多信息参见perl documentation for special variables

      【讨论】:

        猜你喜欢
        • 2020-03-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-13
        • 2021-11-23
        相关资源
        最近更新 更多