Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 用Perl实现哈希和数组_Arrays_Perl_Hash - Fatal编程技术网

Arrays 用Perl实现哈希和数组

Arrays 用Perl实现哈希和数组,arrays,perl,hash,Arrays,Perl,Hash,我有一个.plist文件,Data::Dumper可以看到它 $VAR1 = { 'SPOOLS' => [ 'SPOOL1', 'SPOOL2', 'SPOOL3' ], 'path' => '/usr/local/thanks/for/your/help'

我有一个.plist文件,Data::Dumper可以看到它

$VAR1 = {
          'SPOOLS' => [
                        'SPOOL1',
                        'SPOOL2',
                        'SPOOL3'
                      ],
          'path' => '/usr/local/thanks/for/your/help',
          'contentMatch' => [
                              {
                                'priority' => '1',
                                'match' => '*.hello'
                              },
                              {
                                'match' => '*.guys',
                                'priority' => '2'
                              }
                            ]
        };
要访问Perl中的.plist文件,我使用以下代码:

my $locPlist = "conf.plist";

my $configdict = NSDictionary->dictionaryWithContentsOfFile_($locPlist);
my $plistref   = Foundation::perlRefFromObjectRef($configdict);
my %plist = %{$plistref};
我知道如何使用“$plist{path}”访问“path”,使用“$plist{SPOOLS}[0]”访问SPOOLS数组,但是: 如何将后台文件的数量放入数组中,比如
my@array=$plist{SPOOLS}
以及如何获得“contentMatch”的内容

谢谢你的帮助

编辑: 非常感谢您的帮助,我能够访问数据。但是,有没有更干净的方法从散列中的contentMatch获取内容并直接访问,而不是这样做:

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;
}
最好的, 蒂姆

或者,要深入了解
contentMatch
示例:

foreach ( @{ $plistref->{contentMatch} } ) {
  say "match: $_->{match}";
  say "priority: $_->{priority}";
}
中提供了更多有关在Perl中处理复杂数据结构的信息

更新:您请求以另一种方式访问contentMatch数据的帮助。我不太确定您在问什么-您的示例代码似乎只是将一个散列数组转换为一个散列数组。我认为散列数组是数据更好的表示形式

但这是你的代码和数据。所以我想这就是我写你想做的事情的方式

my %events;
foreach ( @{ $plistref->{contentMatch} } ) {
  push @{ $events{match} }, $_->{match};
  push @{ $events{priority}, $_->{priority};
}
我会更乐意把它作为一个散列数组保存,我已经给了你这个代码

my @content_match = @{ $plistref->{contentMatch} };
我认为理解复杂的数据结构在Perl中的工作方式对于成为一名称职的Perl程序员至关重要。我强烈建议您掌握中的示例

或者,要深入了解
contentMatch
示例:

foreach ( @{ $plistref->{contentMatch} } ) {
  say "match: $_->{match}";
  say "priority: $_->{priority}";
}
中提供了更多有关在Perl中处理复杂数据结构的信息

更新:您请求以另一种方式访问contentMatch数据的帮助。我不太确定您在问什么-您的示例代码似乎只是将一个散列数组转换为一个散列数组。我认为散列数组是数据更好的表示形式

但这是你的代码和数据。所以我想这就是我写你想做的事情的方式

my %events;
foreach ( @{ $plistref->{contentMatch} } ) {
  push @{ $events{match} }, $_->{match};
  push @{ $events{priority}, $_->{priority};
}
我会更乐意把它作为一个散列数组保存,我已经给了你这个代码

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

我认为理解复杂的数据结构在Perl中的工作方式对于成为一名称职的Perl程序员至关重要。我强烈建议您掌握中的示例。

为什么不直接使用引用,而不是对确切的类型大惊小怪呢?
my@array=@{$plist{SPOOLS}
@Thomas请不要将您的问题编辑成“应该删除”来破坏您的问题。如果您有需要删除此内容的问题,请与堆栈溢出版主联系。为什么不使用引用而不是对确切的类型大惊小怪呢?
my@array=@{$plist{SPOOLS}
@Thomas请不要通过编辑问题来污损您的问题,说它“应该被删除”。如果您有需要删除此内容的问题,请联系堆栈溢出版主。非常感谢您的回答,最后一个问题,是否有更干净的方法从哈希中的contentMatch获取内容并直接访问,而不是这样做:my$a=0;我的%事件;foreach({$plistref->{contentMatch}}){$events{match}[$a]=${match};$events{priority}[$a]=${priority};print$events{match}[$a];print$events{priority}[$a];$a=$a+1;}在注释中这样的代码几乎不可能理解。请将此添加到您的问题中。或者(可能更好)问一个新问题。哎呀,非常抱歉,我没意识到它把代码放在了一行中。我编辑了我的帖子非常感谢你的回答,最后一个问题,有没有更干净的方法从contentMatch中获取内容并直接访问,而不是这样做:my$a=0;我的%事件;foreach({$plistref->{contentMatch}}){$events{match}[$a]=${match};$events{priority}[$a]=${priority};print$events{match}[$a];print$events{priority}[$a];$a=$a+1;}在注释中这样的代码几乎不可能理解。请将此添加到您的问题中。或者(可能更好)问一个新问题。哎呀,非常抱歉,我没意识到它把代码放在了一行中。我编辑了我的帖子