gpt4 book ai didi

perl - 使用 perl 解析 YAML 文件。如何访问和解析不同的 yaml 对象。

转载 作者:行者123 更新时间:2023-12-04 00:40:39 24 4
gpt4 key购买 nike

我正在寻找解析 YAML 文档的帮助。具体来说,我不确定主机是否打印/访问“卷”元素。任何帮助将不胜感激。提前致谢!

perl 代码:

#!/usr/bin/perl

use YAML::Tiny;


# Open the config
$yaml = YAML::Tiny->read( 'file.yml' );

# Reading properties
my $root = $yaml->[0]->{rootproperty};
my $one = $yaml->[0]->{physical_interfaces}->{e0a};
my $Foo = $yaml->[0]->{physical_interfaces}->{e0b};
print "$root\n";
print "$one\n";
print "$volume1\n";

我的 yaml 文件看起来像这样:file.yaml

---
rootproperty: netapp1
is_netapp: Yes
netapp_mode: 7mode
is_metro_cluster: Yes
is_vseries: Yes
is_flexcache_origin: No
snapmirror:
enabled: Yes
destination: Yes
lag_threshold: 2300
physical_interfaces:
e0a: netapp1-e0
e0b: netapp1-e1
mgt: netapp1-mgt
volumes:
- volume: vol1
reserve: 50
sched: 6 42 0
- volume: vol2
reserve: 20
sched: 0 3 0

最佳答案

好吧,你似乎已经有了正确的想法。就像您可以使用

访问 rootproperty 字段一样
my $root = $yaml->[0]{rootproperty}

您可以访问 volumes 数组

my $volumes = $yaml->[0]{volumes}

$volumes 现在是对卷哈希数组的引用。例如,您可以使用

for my $vol (@$volumes) {
print $vol->{volume}, "\n";
print $vol->{reserve}, "\n";
print $vol->{sched}, "\n";
print "\n";
}

根据您显示的数据,这将产生输出

vol1
50
6 42 0

vol2
20
0 3 0

关于perl - 使用 perl 解析 YAML 文件。如何访问和解析不同的 yaml 对象。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19118034/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com