Perl 中的自动递增字母 [英] Autoincrementing letters in Perl

查看:29
本文介绍了Perl 中的自动递增字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不懂 Perl 中的自动递增字母.

I do not understand autoincrementing letters in Perl.

这个例子似乎完全可以理解:

This example seems perfectly understandable:

$a = 'bz'; ++$a;
ca #output

b 递增为 c.z 没有什么可去的了,所以它又回到了 a(或者至少我是这样看待这个过程的).

b gets incremented to c. There is nothing left for z to go to, so it goes back to a (or at least this is how I see the process).

但后来我遇到了这样的陈述:

But then I come across statements like this:

$a = 'Zz'; ++$a;
AAa #output

和:

$a = '9z'; ++$a;
10 #output

为什么不递增 Zz 返回 Aa?为什么不递增 9z 返回 0z?

Why doesn't incrementing Zz return Aa? And why doesn't incrementing 9z return 0z?

谢谢!

推荐答案

引用 perlop:

然而,如果变量已经仅在字符串上下文中使用,因为它被设置,并且有一个不是空字符串并匹配模式 /^[a-zA-Z]*[0-9]*z/,增量作为字符串完成,保留其内的每个字符范围,带进位.

If, however, the variable has been used in only string contexts since it was set, and has a value that is not the empty string and matches the pattern /^[a-zA-Z]*[0-9]*z/, the increment is done as a string, preserving each character within its range, with carry.

范围是 0-9、A-Z 和 a-z.当需要一个新字符时,它从第一个字符的范围内取出.每个范围都是独立的;字符永远不会离开它们开始的范围.

The ranges are 0-9, A-Z, and a-z. When a new character is needed, it is taken from the range of the first character. Each range is independent; characters never leave the range they started in.

9z 与模式不匹配,因此它获得了一个数字增量.(它可能应该给出参数不是数字"警告,但在 Perl 5.10.1 中没有.)数字只允许在所有字母(如果有)之后,之前不允许

9z does not match the pattern, so it gets a numeric increment. (It probably ought to give an "Argument isn't numeric" warning, but it doesn't in Perl 5.10.1.) Digits are allowed only after all the letters (if any), never before them.

注意全数字字符串确实匹配模式,并且确实接收字符串增量(如果它从未在数字上下文中使用过).但是,对此类字符串进行字符串增量的结果与数字增量相同,只是它具有无限精度并保留前导零(如果有).(因此,只有当位数超过 IV 或 NV 可以存储的数量,或者它有前导零时,您才能分辨出差异.)

Note that an all-digit string does match the pattern, and does receive a string increment (if it's never been used in a numeric context). However, the result of a string increment on such a string is identical to a numeric increment, except that it has infinite precision and leading zeros (if any) are preserved. (So you can only tell the difference when the number of digits exceeds what an IV or NV can store, or it has leading zeros.)

我不明白您为什么认为 Zz 应该变成 Aa(除非您正在考虑模算术,但事实并非如此).通过这个过程变成了AAa:

I don't see why you think Zz should become Aa (unless you're thinking of modular arithmetic, but this isn't). It becomes AAa through this process:

  1. 递增 z 环绕到 a.增加前一个字符.
  2. 递增 Z 环绕到 A.没有前面的字符,所以添加这个范围内的第一个,也就是另一个A.
  1. Incrementing z wraps around to a. Increment the previous character.
  2. Incrementing Z wraps around to A. There is no previous character, so add the first one from this range, which is another A.

范围运算符 (..), 当给定两个字符串(左侧的字符串与模式匹配)时,使用字符串增量来生成一个列表(这在该部分的末尾附近进行了解释).列表从左边的操作数开始,然后递增,直到:

The range operator (..), when given two strings (and the left-hand one matches the pattern), uses the string increment to produce a list (this is explained near the end of that section). The list starts with the left-hand operand, which is then incremented until either:

  1. 该值等于右手操作数,或
  2. 值的长度超过了右侧操作数的长度.

它返回所有值的列表.(如果情况 2 终止了列表,则最终值不包含在其中.)

It returns a list of all the values. (If case 2 terminated the list, the final value is not included in it.)

这篇关于Perl 中的自动递增字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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