如何确定数组引用中的元素数? [英] How do I determine the number of elements in an array reference?

查看:151
本文介绍了如何确定数组引用中的元素数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我面临的情况...

Here is the situation I am facing...

$perl_scalar = decode_json( encode ('utf8',$line));

decode_json返回一个引用.我确定这是一个数组.如何找到$ perl_scalar的大小?根据Perl文档,使用@name引用数组.有解决方法吗?

decode_json returns a reference. I am sure this is an array. How do I find the size of $perl_scalar?? As per Perl documentation, arrays are referenced using @name. Is there a workaround?

此引用由哈希数组组成.我想获取哈希值的数量.

This reference consist of an array of hashes. I would like to get the number of hashes.

如果我执行length($ perl_scalar),我得到的数字与数组中元素的数量不匹配.

If I do length($perl_scalar), I get some number which does not match the number of elements in array.

推荐答案

那是:

scalar(@{$perl_scalar});

您可以从 perlreftut 中获取更多信息.

You can get more information from perlreftut.

您可以将引用的数组复制到普通数组中,如下所示:

You can copy your referenced array to a normal one like this:

my @array = @{$perl_scalar};

但是在此之前,您应该使用 ref 检查$perl_scalar是否真的在引用数组. :

But before that you should check whether the $perl_scalar is really referencing an array, with ref:

if (ref($perl_scalar) eq "ARRAY") {
  my @array = @{$perl_scalar};
  # ...
}

更新

length 方法不能用于计算数组的长度,它用于获取数组的长度.字符串.

The length method cannot be used to calculate length of arrays, it's for getting the length of the strings.

这篇关于如何确定数组引用中的元素数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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