为什么在Perl中连接数组会产生数字? [英] Why does concatenating arrays in Perl produce numbers?

查看:64
本文介绍了为什么在Perl中连接数组会产生数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是尝试用 + 运算符将Perl中的数组连接起来,结果很奇怪:

I just tried to concatenate arrays in Perl with the + operator and got strange results:

perl -wE 'say([1, 2, 3] + [4, 5, 6])'
73464360

对哈希进行相同操作似乎是语法错误:

Doing the same with hashes seems to be a syntax error:

perl -wE 'say({} + {})'
syntax error at -e line 1, near "} +"
Execution of -e aborted due to compilation errors.

第一个表达式的结果是什么?它记录在任何地方吗?

What is the result of the 1st expression? Is it documented anywhere?

推荐答案

它来自引用的数字化,产生引用的内存地址。

It is from the numification of references, which produces the memory address of the reference.

perl -E 'say \@a; say 0+\@a; printf "%x\n",0+\@a'

典型输出(尽管它可能会在您每次运行该程序时更改)

Typical output (though it may change every time you run the program)

ARRAY(0x1470518)
21431576
1470518      <--- same number as in first line

您的哈希参考示例几乎可用,但Perl似乎是将第一组 {} 块解析为代码块而不是哈希引用。如果您使用一元 + 并强制Perl将其视为哈希引用,则它将起作用。我的意思是工作。

Your hash reference example almost works, but it seems that Perl is parsing the first set of {} blocks as a code block rather than as a hash reference. If you use a unary + and force Perl to treat it as a hash reference, it will work. I mean "work".

perl -E 'say(+{} + {})'
40007168

这篇关于为什么在Perl中连接数组会产生数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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