Alex Alex - 1 month ago 12
Perl Question

Hash and array with Perl

I have a .plist file that can be seen by Data::Dumper into this form

$VAR1 = {
'SPOOLS' => [
'SPOOL1',
'SPOOL2',
'SPOOL3'
],
'path' => '/usr/local/thanks/for/your/help',
'contentMatch' => [
{
'priority' => '1',
'match' => '*.hello'
},
{
'match' => '*.guys',
'priority' => '2'
}
]
};


To access my .plist file in Perl, I use this code:

my $locPlist = "conf.plist";

my $configdict = NSDictionary->dictionaryWithContentsOfFile_($locPlist);
my $plistref = Foundation::perlRefFromObjectRef($configdict);
my %plist = %{$plistref};


I know how to access the "path" with "$plist{path}" and the SPOOLS array with "$plist{SPOOLS}[0]" but:
How can I get the of SPOOLS into an array, something like
my @array = $plist{SPOOLS}
and how can I also get the content of "contentMatch" ?

Thanks for your help!

EDIT:
Thank you so much for your help, I was able to access the data. But is there a cleaner way to get back the content from contentMatch in a hash and access directly instead of doing that:

my $number_matches = scalar @{ $plistref->{contentMatch} } ;
my $a = 0;
my %events;
foreach ( @{ $plistref->{contentMatch} } ) {
$events{match}[$a] = $_->{match};
$events{priority}[$a] = $_->{priority};

$a = $a+1;
}


Best,
Tim.

Answer
my @spools = @{ $plistref->{SPOOLS} };

my @content_match = @{ $plistref->{contentMatch} };

Or, to dig a little deeper into the contentMatch example:

foreach ( @{ $plistref->{contentMatch} } ) {
  say "match: $_->{match}";
  say "priority: $_->{priority}";
}

Plenty more information about dealing with complex data structures in Perl in the Perl Data Structures Cookbook.