Perl:YAML 在数组中迭代? [英] Perl:YAML iterate in the Array?

查看:69
本文介绍了Perl:YAML 在数组中迭代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注这个例子 在 Perl 脚本中使用来自 YAML 配置文件的数据的简单示例

vihtorr@w00w/var/www $ cat test.yaml

vihtorr@w00w /var/www $ cat test.yaml

IPs: [500, 600, 200, 100]

vihtorr@w00w/var/www $ cat yam2.pl

vihtorr@w00w /var/www $ cat yam2.pl

 use strict;
 use warnings;
 use YAML::XS qw(LoadFile); 

 my $settings = LoadFile('test.yaml');
 print "The IPs are ", $settings->{IPs};

我想知道谁在数组中迭代?

and i would like to know who to iterate inside the Array?

当我执行我得到的代码

perl yam2.pl 
The IPs are ARRAY(0x166e5e0)

感谢帮助菜鸟

推荐答案

$settings->{IPs}

保存对数组的引用.使用

holds a reference to an array. Arrays are dereferenced using

@{ $ref }       # The whole thing
${ $ref }[$i]   # One element
$ref->[$i]      # One element
@{ $ref }[@i]   # Array slice

所以你可以使用

@{ $settings->{IPs} }

你得到:

print "The IPs are ", join(', ', @{ $settings->{IPs} }), "\n";

你可能也对

for my $ip (@{ $settings->{IPs} }) {
   ... do something with $ip ...
}

参考文献:

这篇关于Perl:YAML 在数组中迭代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆