引用带有undef条目的单词函数

时间:2012-08-22 19:11:22

标签: perl

在perl中使用引用词时,是否可以在列表中包含undef值?

my @ number_array = qw( 1 2 3 4 5 6 7 8 9 10 )

我想知道是否可以在该列表中添加一个undef值,使其包含11个值而不是10个?

2 个答案:

答案 0 :(得分:5)

perl中没有null,但您可以使用undef。由于qw仅使用以空格分隔的字符串显式操作,因此您必须在qw之外指定它,但您可以在括号内轻松编写多个列表:

my @number_array = (undef, qw( 1 2 3 4 5 6 7 8 9 10 ));
print scalar @number_array;

>11

答案 1 :(得分:5)

qw(...)

相当于

(split(' ', q(...), 0))

至于你的问题的答案,这取决于你的意思是“null”。

  • 是undef?不,split返回字符串。
  • 空字符串?不,split无法返回那些操作数。
  • 零?是。
  • U + 0000?是。

您必须通过其他方式构建列表。例如,

my @array = (qw( 1 2 3 4 5 6 7 8 9 10 ), undef);

甚至类似的

my @array = (1..10, undef);
相关问题