i am following on this example Simple example of using data from a YAML configuration file in a Perl script
vihtorr@w00w /var/www $ cat test.yaml
IPs: [500, 600, 200, 100]
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?
when i execute the code i get
perl yam2.pl
The IPs are ARRAY(0x166e5e0)
thanks for helping a noob
$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
so you can access the array using
@{ $settings->{IPs} }
You get:
print "The IPs are ", join(', ', @{ $settings->{IPs} }), "\n";
You might also be interseted in
for my $ip (@{ $settings->{IPs} }) {
... do something with $ip ...
}
References:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With