在 Perl 中,如何遍历 YAML 加载的哈希数组?

发布于 2024-09-17 09:52:45 字数 533 浏览 6 评论 0原文

我将以下 YAML 流加载到 Perl 数组中,并且想要遍历与 Field2 关联的数组。

use YAML;

my @arr = Load(<<'...');
---
Field1: F1
Field2:
 - {Key: v1, Val: v2}
 - {Key: v3, Val: v4}
---
Field1: F2
Field2:
 - {Key: v5, Val: v6}
 - {Key: v7, Val: v8}
...

foreach (@arr) {
    @tmp = $_->{'Field2'};   
    print $#tmp; # why it says 0 when I have 2 elements?

    # Also why does the below loop not work? 
    foreach ($_->{'Field2'}) {
    print $_->{'Key'} . " -> " $_->{'Val'} . "\n";
 }
}

我很感激任何反馈。谢谢。

I load the following YAML stream into a Perl's array and I want to traverse the array associated with Field2.

use YAML;

my @arr = Load(<<'...');
---
Field1: F1
Field2:
 - {Key: v1, Val: v2}
 - {Key: v3, Val: v4}
---
Field1: F2
Field2:
 - {Key: v5, Val: v6}
 - {Key: v7, Val: v8}
...

foreach (@arr) {
    @tmp = $_->{'Field2'};   
    print $#tmp; # why it says 0 when I have 2 elements?

    # Also why does the below loop not work? 
    foreach ($_->{'Field2'}) {
    print $_->{'Key'} . " -> " $_->{'Val'} . "\n";
 }
}

I appreciate any feedback. Thank you.

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

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

发布评论

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

评论(2

土豪我们做朋友吧 2024-09-24 09:52:45

因为你没有正确使用参考文献。您可能需要重读 perldoc perlreftutperldoc perlref

#!/usr/bin/perl

use strict;
use warnings;

use YAML;

my @arr = Load(<<'...');
---
Field1: F1
Field2:
 - {Key: v1, Val: v2}
 - {Key: v3, Val: v4}
---
Field1: F2
Field2:
 - {Key: v5, Val: v6}
 - {Key: v7, Val: v8}
...

for my $record (@arr) {
     print "$record->{Field1}:\n";
     for my $subrecord (@{$record->{Field2}}) {
         print "\t$subrecord->{Key} = $subrecord->{Val}\n";
     }
}

Because you are not using references correctly. You may want to reread perldoc perlreftut and perldoc perlref.

#!/usr/bin/perl

use strict;
use warnings;

use YAML;

my @arr = Load(<<'...');
---
Field1: F1
Field2:
 - {Key: v1, Val: v2}
 - {Key: v3, Val: v4}
---
Field1: F2
Field2:
 - {Key: v5, Val: v6}
 - {Key: v7, Val: v8}
...

for my $record (@arr) {
     print "$record->{Field1}:\n";
     for my $subrecord (@{$record->{Field2}}) {
         print "\t$subrecord->{Key} = $subrecord->{Val}\n";
     }
}
往事随风而去 2024-09-24 09:52:45

您需要对数据结构和引用进行一些练习。这有效:

use 5.010;
use strict;
use warnings FATAL => 'all';

use YAML qw(Load);

my @structs = Load(<<'...');
---
Field1: F1
Field2:
 - {Key: v1, Val: v2}
 - {Key: v3, Val: v4}
---
Field1: F2
Field2:
 - {Key: v5, Val: v6}
 - {Key: v7, Val: v8}
...

# (
#     {
#         Field1 => 'F1',
#         Field2 => [
#             {
#                 Key => 'v1',
#                 Val => 'v2'
#             },
#             {
#                 Key => 'v3',
#                 Val => 'v4'
#             }
#         ]
#     },
#     {
#         Field1 => 'F2',
#         Field2 => [
#             {
#                 Key => 'v5',
#                 Val => 'v6'
#             },
#             {
#                 Key => 'v7',
#                 Val => 'v8'
#             }
#         ]
#     }
# )

foreach (@structs) {
    my $f2_aref = $_->{'Field2'};
    print scalar @{ $f2_aref }; # 2

    foreach (@{ $f2_aref }) {
        say sprintf '%s -> %s', $_->{'Key'}, $_->{'Val'};
    }

#     v1 -> v2
#     v3 -> v4
#     v5 -> v6
#     v7 -> v8
}

You need to do some exercises with data structures and references. This works:

use 5.010;
use strict;
use warnings FATAL => 'all';

use YAML qw(Load);

my @structs = Load(<<'...');
---
Field1: F1
Field2:
 - {Key: v1, Val: v2}
 - {Key: v3, Val: v4}
---
Field1: F2
Field2:
 - {Key: v5, Val: v6}
 - {Key: v7, Val: v8}
...

# (
#     {
#         Field1 => 'F1',
#         Field2 => [
#             {
#                 Key => 'v1',
#                 Val => 'v2'
#             },
#             {
#                 Key => 'v3',
#                 Val => 'v4'
#             }
#         ]
#     },
#     {
#         Field1 => 'F2',
#         Field2 => [
#             {
#                 Key => 'v5',
#                 Val => 'v6'
#             },
#             {
#                 Key => 'v7',
#                 Val => 'v8'
#             }
#         ]
#     }
# )

foreach (@structs) {
    my $f2_aref = $_->{'Field2'};
    print scalar @{ $f2_aref }; # 2

    foreach (@{ $f2_aref }) {
        say sprintf '%s -> %s', $_->{'Key'}, $_->{'Val'};
    }

#     v1 -> v2
#     v3 -> v4
#     v5 -> v6
#     v7 -> v8
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文