Perl 中的递增 (++) 和递减 (--) 字符串 [英] Increment (++) and decrement (--) strings in Perl

查看:66
本文介绍了Perl 中的递增 (++) 和递减 (--) 字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 perl -e '$string="a";print ++$string;' 我们得到 b,
但是使用 perl -e '$string="b";print --$string;' 我们得到 -1.

With perl -e '$string="a";print ++$string;' we get b,
but with perl -e '$string="b";print --$string;' we get -1.

那么,如果我们可以增加为什么不能减少呢?

So, if we can increment why can't we decrement?

已编辑
自动递减运算符并不神奇" by perlop

Perl 为我们提供了很多便利,为什么没有这个呢?这不是批评,但类似的运营商不会有类似的行为吗?有什么特殊原因吗?

Perl give us lots of facilities, why not this one? This is not criticism, but wouldn't be expected similar behavior for similar operators? Is there any special reason?

推荐答案

perlop(1) 解释了这是真的,但没有给出理由:

perlop(1) explains that this is true, but doesn't give a rationale:

自增运算符有一些额外的内置魔法.[如果适用,并受某些限制,]增量作为字符串完成,保留其范围内的每个字符,并带有进位[...]

The auto-increment operator has a little extra builtin magic to it. [If applicable, and subject to certain constraints,] the increment is done as a string, preserving each character within its range, with carry[...]

自动递减运算符并不神奇.

The auto-decrement operator is not magical.

你得到 -1 的原因是因为当解释为一个数字时,b"变成了 0,因为它没有前导数字(相反,4b"变成 4).

The reason you get -1 is because when interpreted as a number, "b" turns into 0 since it has no leading digits (Contrarily, "4b" turns into 4).

这篇关于Perl 中的递增 (++) 和递减 (--) 字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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