在Perl中,美元和数字符号如何一起工作? [英] How do dollar and number sign together work in perl?

查看:95
本文介绍了在Perl中,美元和数字符号如何一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我遇到了一个问题,需要我确定perl中数组的最大索引.我曾经这样做:

Today I have encountered a problem that required me to determine the maximum index of an array in perl. I used to do it this way:

my @array = (1, 2, 3);
print $array[@array - 1];

但是今天我偶然发现了以下代码:

But today I have stumbled upon this code:

my @array = (1, 2, 3);
print $array[$#array];

在此文档上,我找不到任何东西. $#构造到底是什么?那是运营商吗?而且它是如何工作的,它比第一段代码要快吗?是否总是返回最大数组索引?是否已弃用?

I couldn't find anything on that matter in the docs. What exactly is that $# construct? Is that an operator? And how does it work, is it faster than the first piece of code? Does it always return the maximum array index? Is it deprecated or not?

我知道有很多问题,但是可以将它们全部归结为一个,这就是我真正想知道的:它是如何工作的?

I know that's a lot of questions, but they all can be summed up by one, and that's what I really want to know: How does it work?

推荐答案

perldoc perldata中对此进行了说明,标量值"部分.简而言之,$#array@array的最后一个索引.至于它是如何工作的,它有点像一个运算符,但是只有$@才是运算符.将其视为特殊语法.数组的最后一个索引恰好是有一个名字".您可以读取并分配该变量.

This is documented in perldoc perldata, section "Scalar Values". In short, $#array is the last index of @array. As for how it works — it's sort of like an operator, but only as much as $ and @ are operators. Think of it as special syntax. The last index of an array just happens to "have a name". It's a variable that you can read from and assign to.

这篇关于在Perl中,美元和数字符号如何一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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