Perl 字符串自动增量无法像以前一样工作
我有一些代码,用于转换平面文件中的一些数据元素。我将旧值:新值保存到哈希值中,该哈希值在处理结束时写入文件中。在后续执行时,我重新加载到哈希中,以便可以在其他数据文件上重用之前转换的值。我还保存了最后一个转换值,因此如果遇到未转换的值,我可以为其分配一个新的转换值并将其添加到哈希中。
我之前(二月份)曾在六个文件上使用过这段代码,没有出现任何问题。我有一个设置为 ZCKL0 (最后一个字符为零)的变量,该变量是从保存上次使用的值的文件中检索的。我应用增量运算符 ... $数据{$olddata} = ++$dataseed; ... $dataseed 中的结果值为 1,而不是 ZCKL1。最初的起始种子值为 ZAAA0。
我在这里缺少什么?
I have some code where I am converting some data elements in a flat file. I save the old:new values to a hash which is written to a file at the end of processing. On subsequence execution, I reload into a hash so I can reuse previously converted values on additional data files. I also save the last conversion value so if I encounter an unconverted value, I can assign it a new converted value and add it to the hash.
I had used this code before (back in Feb) on six files with no issues. I have a variable that is set to ZCKL0 (last character is a zero) which is retrieved from a file holding the last used value. I apply the increment operator
...
$data{$olddata} = ++$dataseed;
...
and the resultant value in $dataseed is 1 instead of ZCKL1. The original starting seed value was ZAAA0.
What am I missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否在代码的数字上下文中使用
$dataseed
变量?来自 perlop:
Do you use the
$dataseed
variable in a numeric context in your code?From perlop:
如前所述,字符串上的 ++ 很“神奇”,因为它根据字符串的内容和使用该字符串的上下文进行不同的操作。
为了说明问题并假设:
then
将打印:
while
打印
注意:在其他流行的编程语言中,++ 仅对于数字值是合法的。
不鼓励使用 Perl 的非直观、“神奇”功能,因为它们会导致混乱且可能无法支持的代码。
As prevously mentioned, ++ on strings is "magic" in that it operates differently based on the content of the string and the context in which the string is used.
To illustrate the problem and assuming:
then
will print:
while
prints
NB: In other popular programming languages, the ++ is legal for numeric values only.
Using non-intuitive, "magic" features of Perl is discouraged as they lead to confusing and possibly unsupportable code.
您可以几乎同样简洁地编写此代码,而无需依赖神奇的
++
行为:e
标志使其成为表达式替换。You can write this almost as succinctly without relying on the magic
++
behavior:The
e
flag makes it an expression substitution.