Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl:YAML iterate in the Array?

Tags:

yaml

perl

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

like image 438
pacv Avatar asked Nov 29 '25 10:11

pacv


1 Answers

$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:

  • Mini-Tutorial: Dereferencing Syntax
  • References Quick Reference
  • perlreftut
  • perlref
  • perldsc
  • perllol
like image 131
ikegami Avatar answered Dec 02 '25 05:12

ikegami