快速提问...
为什么第一个键可以工作,而其他键却不能工作?也就是说,第二个键会引发语法错误。我以前使用过数字作为键,但是一旦我写入“to
”,脚本就会变成黑色(也就是说,不是通常用于键的注释颜色)。如果我去掉“to
”,它就会起作用,并在下一个键上抛出错误。
我可以不使用以数字开头的数字和字母组合吗?
my %ranges = (
under10 => "x < 10000",
10to20 => "10000 <= x < 20000",
20to30 => "20000 <= x < 30000",
30to40 => "30000 <= x < 40000",
40to50 => "40000 <= x < 50000",
50to60 => "50000 <= x < 60000",
60to70 => "60000 <= x < 70000",
70to80 => "70000 <= x < 80000",
80to90 => "80000 <= x < 90000",
90to100 => "90000 <= x < 100000",
100plus => "100000 <= x",
);
最佳答案
将它们放在引号中。 documentation说:
The => operator is mostly just a more visually distinctive synonym for a comma, but it also arranges for its left-hand operand to be interpreted as a string if it's a bareword that would be a legal simple identifier.
标识符必须以字母或下划线开头,因此 10to30
不是合法的标识符。因此,它不会转换为字符串。
关于perl - 在某些情况下不能使用数字作为 Perl 哈希键吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16037290/