如何从perl中的哈希内部的数组中打印一个元素

发布于 2025-02-09 14:34:01 字数 947 浏览 1 评论 0原文

我正在尝试打印以多维格式的API打印输出。

use strict;
use warnings;
use Data::Dumper;

my $content={
  'school_set' => 'SSET1234',
  'result' => [
    {
      'school_name' => 'school_abc',
      'display_value' => 'IL25',
      'school_link' => 'example.com',
      'status' => 'registerd',
      'status_message' => 'only arts',
      'school_id' => '58c388d40596191f',
    }
  ],
  'school_table' => 'arts_schools'
};

print "school_name is=".$content{result}[0]{school_name};
print "school_status is=".$content{result}[3]{status};
output
Global symbol "%content" requires explicit package name (did you forget to declare "my %content"?) at test8.pl line 20.
Global symbol "%content" requires explicit package name (did you forget to declare "my %content"?) at test8.pl line 21.

从结果开始,我必须打印下面的输出。

school_name = school_abc
school_status = registered

I'm trying to print the outputs from an API which are in multidimensional format.

use strict;
use warnings;
use Data::Dumper;

my $content={
  'school_set' => 'SSET1234',
  'result' => [
    {
      'school_name' => 'school_abc',
      'display_value' => 'IL25',
      'school_link' => 'example.com',
      'status' => 'registerd',
      'status_message' => 'only arts',
      'school_id' => '58c388d40596191f',
    }
  ],
  'school_table' => 'arts_schools'
};

print "school_name is=".$content{result}[0]{school_name};
print "school_status is=".$content{result}[3]{status};
output
Global symbol "%content" requires explicit package name (did you forget to declare "my %content"?) at test8.pl line 20.
Global symbol "%content" requires explicit package name (did you forget to declare "my %content"?) at test8.pl line 21.

I have to print the outputs like below from the result.

school_name = school_abc
school_status = registered

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

姐不稀罕 2025-02-16 14:34:01

如果$ content是哈希参考,则需要首先将其取消。为此使用箭头操作员:

$content->{result}[0]{school_name}

只有%content的语法才有可能。

my %content = ( result => [ { school_name => 'abc' } ] );
print $content{result}[0]{school_name};

如果要打印所有结果,则必须以某种方式循环循环。例如

#!/usr/bin/perl
use warnings;
use strict;

my $content = {
    'result' => [
        {
            'school_name' => 'school_abc',
            'status' => 'registerd',
        },
        {
            'school_name' => 'school_def',
            'status' => 'pending',
        }
    ],
};

for my $school (@{ $content->{result} }) {
    print "school_name is $school->{school_name}, status is $school->{status}\n";

}

If $content is a hash reference, you need to dereference it first. Use the arrow operator for that:

$content->{result}[0]{school_name}

The syntax without the arrow is only possible for %content.

my %content = ( result => [ { school_name => 'abc' } ] );
print $content{result}[0]{school_name};

If you want to print all the results, you have to loop over the array somehow. For example

#!/usr/bin/perl
use warnings;
use strict;

my $content = {
    'result' => [
        {
            'school_name' => 'school_abc',
            'status' => 'registerd',
        },
        {
            'school_name' => 'school_def',
            'status' => 'pending',
        }
    ],
};

for my $school (@{ $content->{result} }) {
    print "school_name is $school->{school_name}, status is $school->{status}\n";

}
a√萤火虫的光℡ 2025-02-16 14:34:01

您的数据结构假定数组,也许将循环输出用于感兴趣的数据很有用。

显示为哈希参考的数据,将需要取消参考才能通过数组循环。

以下代码段是基于您发布的代码,并演示了如何实现所需的输出。

use strict;
use warnings;
use feature 'say';

my $dataset = {
              'school_set' => 'SSET1234',
              'result' => [
                {
                  'school_name' => 'school_abc',
                  'display_value' => 'IL25',
                  'school_link' => 'example.com',
                  'status' => 'registerd',
                  'status_message' => 'only arts',
                  'school_id' => '58c388d40596191f',
                }
              ],
              'school_table' => 'arts_schools'
            };

for my $item ( @{$dataset->{result}} ) {
    say "school_name is   = $item->{school_name}\n"
      . "school_status is = $item->{status}";
}

exit 0;

输出

school_name is   = school_abc
school_status is = registerd

Your data structure assumes an array, perhaps it would be useful to utilize loop output for the data of interest.

The data presented as hash reference and will require de-referencing to loop through an array.

Following code snippet is based on your posted code and demonstrates how desired output can be achieved.

use strict;
use warnings;
use feature 'say';

my $dataset = {
              'school_set' => 'SSET1234',
              'result' => [
                {
                  'school_name' => 'school_abc',
                  'display_value' => 'IL25',
                  'school_link' => 'example.com',
                  'status' => 'registerd',
                  'status_message' => 'only arts',
                  'school_id' => '58c388d40596191f',
                }
              ],
              'school_table' => 'arts_schools'
            };

for my $item ( @{$dataset->{result}} ) {
    say "school_name is   = $item->{school_name}\n"
      . "school_status is = $item->{status}";
}

exit 0;

Output

school_name is   = school_abc
school_status is = registerd
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文