请求帮助以了解给定代码中..运算符的行为

时间:2014-02-28 11:25:44

标签: perl

当我运行下面显示的代码时

use 5.14.0;
print("\n");
print(join("  ", ("A01".."A10")), "\n");
print("\n");
print(join("  ", ("A_01".."A_10")), "\n");
print("\n");

我得到了

A01  A02  A03  A04  A05  A06  A07  A08  A09  A10

A_01

uname -a的输出粘贴在下面。

Linux n51958 3.2.0-58-generic #88-Ubuntu SMP Tue Dec 3 17:37:58 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

为什么from和to字符串中的下划线会产生这样的差异?

由于

2 个答案:

答案 0 :(得分:6)

perldoc perlop

中描述了此行为
The range operator (in list context) makes use of the magical auto-increment 
algorithm if the operands are strings. You can say

    @alphabet = ("A" .. "Z");

to get all normal letters of the English alphabet, 

...

If the initial value specified isn't part of a magical increment sequence (that 
is, a non-empty string matching /^[a-zA-Z]*[0-9]*\z/ ), only the initial value 
will be returned. 

答案 1 :(得分:2)

从运算符上的perl doc(http://perldoc.perl.org/perlop.html - 标题“范围运算符”下):

  

如果指定的初始值不是魔法增量序列的一部分(即,非空字符串匹配/ ^ [a-zA-Z] [0-9] \ z /),只返回初始值。所以以下只会返回一个alpha

也许您可能想要在输出上使用正则表达式将“A”替换为“A_”,或者稍后再加入“A”。