$#array 在 Perl 中是什么意思? [英] What does $#array mean in Perl?

查看:42
本文介绍了$#array 在 Perl 中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看以下代码片段:

I'm looking at the following code snippet:

my @ret = <someMethod>
return (undef) if( $DB_ERROR );
return (undef) unless ($#ret >= 0);

$# 是否只是为您提供数组中元素的计数?

Does $# just give you a count of elements in a array?

推荐答案

$#arrayname 给你最后一个元素的索引,所以如果数组 @ret 有 2元素则 $#ret 为 1.

$#arrayname gives you the index of the last element, so if array @ret has 2 elements then $#ret is 1.

而且,正如 Barry Brown 所指出的,空数组给出 -1.

And, as noted by Barry Brown, an empty array gives -1.

要获得长度,您可以在标量上下文中使用数组:

To get the length you can use the array in scalar context:

print scalar @ret;

这篇关于$#array 在 Perl 中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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