在Perl中查找嵌套数组的长度

huangapple go评论65阅读模式
英文:

find nested array length in perl

问题

I have deference array as given below -

"message": [
    {
      "abc": "",
      "xyz": "",
      "pqr": ""
    },
    {
      "abc": "",
      "xyz": "",
      "pqr": ""
    },
    {
      "abc": "",
      "xyz": "",
      "pqr": ""
    },
    {
     "abc": "",
      "xyz": "",
      "pqr": ""
    },
    {
     "abc": "",
      "xyz": "",
      "pqr": ""
    }
  ]

To find its length I tried following stuff but in each case I got answer as '1' instead of '5'.

1. say scalar @message;
2. my $attempt_length = @message;

It might be a very basic question but I am stuck here very badly. Please, help me in finding its length in Perl if anyone can.
Thanks in advance.

英文:

I have deference array as given below -

"message": [
    {
      "abc": "",
      "xyz": "",
      "pqr": ""
    },
    {
      "abc": "",
      "xyz": "",
      "pqr": ""
    },
    {
      "abc": "",
      "xyz": "",
      "pqr": ""
    },
    {
     "abc": "",
      "xyz": "",
      "pqr": ""
    },
    {
     "abc": "",
      "xyz": "",
      "pqr": ""
    }
  ]

To find its length I tried following stuff but in each case I got answer as '1' instead of '5'.

1. say scalar @message;
2. my $attempt_length = @message;

It might be a very basic question but I am stuck here very badly. Please, help me in finding its length in Perl if anyone can.
Thanks in advance.

答案1

得分: 2

以下是翻译好的部分:

当您有一个变量的引用而不是变量名称时,只需将您希望使用的语法中的名称替换为一个块,该块会求值为引用。

如果您有一个数组的名称,您将使用以下内容来获取其大小:

@名称             # 在标量上下文中

如果您有一个数组的引用,您将使用以下内容来获取其大小:

@块            # 在标量上下文中

所以,如果 $messages 包含一个数组的引用,以下内容将获取其大小。

@{ $messages }    # 在标量上下文中

如果它们包含一个简单的标量($名称),您可以省略花括号。

@$messages        # 在标量上下文中

所以,

use Cpanel::JSON::XS qw( decode_json );

my $json = do { local $/; <DATA> };
my $data = decode_json($json);
my $messages = $data->{message};
my $num_messages = @$messages;    # 或者:  @{ $data->{message} }

__DATA__
{
  "message": [
    {
      "abc": "",
      "xyz": "",
      "pqr": ""
    },
    {
      "abc": "",
      "xyz": "",
      "pqr": ""
    },
    {
      "abc": "",
      "xyz": "",
      "pqr": ""
    },
    {
     "abc": "",
      "xyz": "",
      "pqr": ""
    },
    {
     "abc": "",
      "xyz": "",
      "pqr": ""
    }
  ]
}

请参阅Perl解引用语法

英文:

When you have a reference to a variable instead of a variable name, you simply replace the name in the syntax you want to use with a block that evaluate to the reference.

If you have the name of an array, you'd use the following to get its size:

@NAME             # In scalar context

If you have a reference to an array, you'd use the following to get its size:

@BLOCK            # In scalar context

So, if $messages contains a reference to an array, the following will get its size.

@{ $messages }    # In scalar context

You can omit the curlies if they contain a simple scalar ($NAME).

@$messages        # In scalar context

So,

use Cpanel::JSON::XS qw( decode_json );

my $json = do { local $/; <DATA> };
my $data = decode_json($json);
my $messages = $data->{message};
my $num_messages = @$messages;    # Or:  @{ $data->{message} }

__DATA__
{
  "message": [
    {
      "abc": "",
      "xyz": "",
      "pqr": ""
    },
    {
      "abc": "",
      "xyz": "",
      "pqr": ""
    },
    {
      "abc": "",
      "xyz": "",
      "pqr": ""
    },
    {
     "abc": "",
      "xyz": "",
      "pqr": ""
    },
    {
     "abc": "",
      "xyz": "",
      "pqr": ""
    }
  ]
}

See Perl Dereferencing Syntax.

答案2

得分: 0

除了ikegami已经提到的@$arrayref@{ $arrayref }语法之外,我想提一下后缀解引用(自perl v5.24起可用,或自perl v5.20起使用use experimental 'postderef'

my $num_of_elems = $arrayref->@*;         # 隐式标量上下文
my $num_of_elems = scalar $arrayref->@*;  # 显式标量上下文
英文:

In addition to the @$arrayref and @{ $arrayref } syntaxes ikegami already mentioned, I want to mention postfix dereferencing (available since perl v5.24, or with use experimental 'postderef' since perl v5.20)

my $num_of_elems = $arrayref->@*;         # implicit scalar context
my $num_of_elems = scalar $arrayref->@*;  # explicit scalar context

huangapple
  • 本文由 发表于 2020年1月6日 17:32:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609633.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定