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

查看:104
本文介绍了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天全站免登陆