在Perl中查找数组的大小 [英] Find size of an array in Perl

查看:70
本文介绍了在Perl中查找数组的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎遇到了几种不同的方法来找到数组的大小.这三种方法有什么区别?

I seem to have come across several different ways to find the size of an array. What is the difference between these three methods?

my @arr = (2);
print scalar @arr; # First way to print array size

print $#arr; # Second way to print array size

my $arrSize = @arr;
print $arrSize; # Third way to print array size

推荐答案

第一种和第三种方法是相同的:它们在标量上下文中评估数组.我认为这是获取数组大小的标准方法.

The first and third ways are the same: they evaluate an array in scalar context. I would consider this to be the standard way to get an array's size.

第二种方法实际上返回数组的最后一个索引,该索引(通常)与数组大小不同.

The second way actually returns the last index of the array, which is not (usually) the same as the array size.

这篇关于在Perl中查找数组的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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