我在解析以下config.yml文件时需要一些帮助:
yamlFile:
map:
"[abcd.txt]":
- name: John
- city: Omaha
"[efgh.txt]":
- name: Sandra
- city: Columbus
"[ijkl.txt]":
- name: Annie
- city: Chicago
我正在使用以下脚本进行解析。
my $config = LoadFile('config.yml');
use Data::Dumper;
for my $conf ( keys %$config ) {
print "$conf\n";
my $map = $config->{$conf};
for my $map1 ( keys %$map ) {
print "$map1\n";
}
}
这将打印以下内容:
yamlFile
map
由于$ map1的键不同,如何进一步获取每个元素的名称和城市?
答案 0 :(得分:1)
以下是如何访问YAML配置文件中项目的示例:
use feature qw(say);
use strict;
use warnings;
use YAML qw(LoadFile);
my $config = LoadFile('config.yml');
my $map_hash = $config->{yamlFile}{map};
for my $conf ( keys %$map_hash ) {
say $conf;
my $elems = $map_hash->{$conf};
for my $map1 ( @$elems ) {
for my $data ( keys %$map1 ) {
say "$data:", $map1->{$data};
}
}
}
输出:
[ijkl.txt]
name:Annie
city:Chicago
[abcd.txt]
name:John
city:Omaha
[efgh.txt]
name:Sandra
city:Columbus