在双引号内打印数组 [英] Printing an array within double quotes

查看:74
本文介绍了在双引号内打印数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

my @a = (1,2,3,4,5);

print @a;      #output: 12345

print "\n";

print "@a";    #output: 1 2 3 4 5

通过将其名称放在双引号中来打印数组会在输出时在每个索引的值之间放置一个空格.它是如何做到的?为什么 print @a; 不打印相同的内容?这两种类型的需求是什么?我的意思是你什么时候使用 print @a; 而不是 print "@a"; ,反之亦然.

Printing an array by putting its name withing double quotes puts a space between each index's value at output. How does it do it? Why doesn't print @a; prints the same? What's the need of both types? I mean when will you use print @a; instead of print "@a"; and vice versa.

推荐答案

更好的问题:为什么不打印类似 array{0x1232ef} 的内容.Print 假设打印字符串输出,@a 不是标量.

Even better question: Why doesn't it print something like array{0x1232ef}. Print is suppose to print a string output and @a isn't a scalar.

哎呀,甚至更好:这是一个标量上下文,所以为什么不打印 5 这是数组中元素的数量.方法如下:

Heck, even better: This is a scalar context, so why not print 5 which is the number of elements in the array. This is how:

print scalar @a;

会打印.

取而代之的是,print 命令采取了一些自由尝试去做你想要做的事情,而不是你所说的.

Instead, the print command is taking some liberties to try to do what you intended and not what you said you want.

我们来看看这个小程序:

Let's take a look at this little program:

@a = qw(a b c d e);

print "@a";         #prints "a b c d e"
print "\n";

print @a;           #prints "abcde"
print "\n";

print @a . "\n";    #prints "5"

print scalar @a;    #prints "5"

注意 print @a 打印 abcde,但如果我在末尾添加 \n,它会打印 @a 在标量上下文中.

Notice that print @a prints abcde, but if I add a \n on the end, it then prints @a in a scalar context.

看看 Perldoc on print(试试命令 perldoc -f print.在大多数系统上,完整的 Perl 文档可以通过 perldoc)

Take a look at the Perldoc on print (try the command perldoc -f print. On most systems, the entire Perl documentation is available via perldoc)

* print LIST
* print

Prints a string or a list of strings. Returns true if successful[...]

啊!如果给定一个列表,它将打印一个字符串列表.

Ah! If given a list, it'll print a list of strings.

$,(如果有)的当前值打印在每个 LIST 项目之间.$\ 的当前值(如果有)在整个 LIST 打印后打印.因为 print 需要一个 LIST,所以 LIST 中的任何内容都会在列表上下文中进行评估,包括您将其返回列表传递给 print 的任何子例程.

The current value of $, (if any) is printed between each LIST item. The current value of $\ (if any) is printed after the entire LIST has been printed. Because print takes a LIST, anything in the LIST is evaluated in list context, including any subroutines whose return lists you pass to print.

让我们尝试一个新程序:

Let's try a new program:

@a = qw(a b c d e);

$, = "--";
print "@a";         #prints "a b c d e"
print "\n";

print @a;           #prints "a--b--c--d--e"
print "\n";

print @a . "\n";    #prints "5"

print scalar @a;    #prints "5"

嗯... $, 在列表元素之间添加了双破折号,但它不会影响引号中的 @a.并且,如果perldoc 中提到了$,,为什么每个人都在喋喋不休地谈论$"?

Hmmm... The $, added the double dashes between the list elements, but it didn't affect the @a in quotes. And, if $, is mentioned in the perldoc, why is everyone prattling about $"?

我们来看看perldoc perlvar

* $LIST_SEPARATOR
* $"

When an array or an array slice is interpolated into a double-quoted string or a
similar context such as /.../ , its elements are separated by this value. Default
is a space. For example, this:

print "The array is: @array\n";

is equivalent to this:

print "The array is: " . join($", @array) . "\n";

Mnemonic: works in double-quoted context.

所以,这就解释了一切!

So, that explains everything!

$" 的默认值是单个空格,而 $, 的默认值是 null.这就是为什么我们得到了我们得到的!

The default of $" is a single space, and the default of $, is null. That's why we got what we got!

还有一个程序...

@a = qw(a b c d e);

$, = "--";
$" = "++";
print "@a";         #prints "a++b++c++d++e"
print "\n";

print @a;           #prints "a--b--c--d--e"
print "\n";

print @a . "\n";    #prints "5"
print scalar @a;    #prints "5"

这篇关于在双引号内打印数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆