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_Reference_Perl Data Structures - Fatal编程技术网

Arrays 我不知道';我不理解这个Perl语法,有人知道吗?

Arrays 我不知道';我不理解这个Perl语法,有人知道吗?,arrays,perl,reference,perl-data-structures,Arrays,Perl,Reference,Perl Data Structures,我从一个Perl插件中得到了这一部分。我不明白它是干什么的。它是关联数组的数组吗?如果是这样的话,那么它不应该以@开始吗?有人能解释一下这个问题吗 my $arguments = [ { 'name' => "process_exp", 'desc' => "{BasePlugin.process_exp}", 'type' => "regexp", 'deft' => &get_default_process_exp(), '

我从一个Perl插件中得到了这一部分。我不明白它是干什么的。它是关联数组的数组吗?如果是这样的话,那么它不应该以@开始吗?有人能解释一下这个问题吗

my $arguments =
  [ { 'name' => "process_exp",
    'desc' => "{BasePlugin.process_exp}",
    'type' => "regexp",
    'deft' => &get_default_process_exp(),
    'reqd' => "no" },
  { 'name' => "assoc_images",
    'desc' => "{MP4Plugin.assoc_images}",
    'type' => "flag",
    'deft' => "",
    'reqd' => "no" },
  { 'name' => "applet_metadata",
    'desc' => "{MP4Plugin.applet_metadata}",
    'type' => "flag",
    'deft' => "" },
  { 'name' => "metadata_fields",
    'desc' => "{MP4Plugin.metadata_fields}",
    'type' => "string",
    'deft' => "Title,Artist,Genre" },
  { 'name' => "file_rename_method",
    'desc' => "{BasePlugin.file_rename_method}",
    'type' => "enum",
    'deft' => &get_default_file_rename_method(), # by default rename imported files and assoc files using this encoding
    'list' => $BasePlugin::file_rename_method_list,
    'reqd' => "no"
  } ];

$arguments
是数组引用(指向数组的引用/指针)

使用
()
初始化数组,使用
[]
初始化数组引用

my @array = ( 1, 2, 3 );
my $array_ref = [ 1, 2, 3 ];
您可以使用
\

my $other_array_ref = \@array;
当您使用数组引用时,将在使用时取消对其引用:

for my $element ( @{$array_ref} )

参见
man perlref

回到您的问题:
$arguments
是对散列引用数组的引用(用
{}
初始化)

看起来像一个引用

您可能需要像这样取消引用

%newhash=%{$arguments}

并将数据打印为


print$newhash{'name'}

正如Bwmat所说,它是对散列引用数组的引用。阅读

$ man perlref

如果你想了解更多的参考资料

%{ $hash_reference }
顺便说一下,在Perl中的五个单词中,您可以执行以下操作:

@array = ( 1, 2 );          # declare an array
$array_reference = \@array; # take the reference to that array
$array_reference->[0] = 2;  # overwrite 1st position of @array

$numbers = [ 3, 4 ];        # this is another valid array ref declaration. Note [ ] instead of ( )
%hash = ( foo => 1, bar => 2 );
$hash_reference = \%hash; 
$hash_reference->{foo} = 2;

$langs = { perl => 'cool', php => 'ugly' }; # this is another valid hash ref declaration. Note { } instead of ( )
print $_, "\n" foreach ( keys %{ $langs } );
哈希也会发生同样的情况

顺便说一下,在Perl中的五个单词中,您可以执行以下操作:

@array = ( 1, 2 );          # declare an array
$array_reference = \@array; # take the reference to that array
$array_reference->[0] = 2;  # overwrite 1st position of @array

$numbers = [ 3, 4 ];        # this is another valid array ref declaration. Note [ ] instead of ( )
%hash = ( foo => 1, bar => 2 );
$hash_reference = \%hash; 
$hash_reference->{foo} = 2;

$langs = { perl => 'cool', php => 'ugly' }; # this is another valid hash ref declaration. Note { } instead of ( )
print $_, "\n" foreach ( keys %{ $langs } );
而且。。。是的,您可以取消引用这些引用

%{ $hash_reference }
将被视为散列,因此如果要打印上面
$langs
的键,可以执行以下操作:

@array = ( 1, 2 );          # declare an array
$array_reference = \@array; # take the reference to that array
$array_reference->[0] = 2;  # overwrite 1st position of @array

$numbers = [ 3, 4 ];        # this is another valid array ref declaration. Note [ ] instead of ( )
%hash = ( foo => 1, bar => 2 );
$hash_reference = \%hash; 
$hash_reference->{foo} = 2;

$langs = { perl => 'cool', php => 'ugly' }; # this is another valid hash ref declaration. Note { } instead of ( )
print $_, "\n" foreach ( keys %{ $langs } );
要取消对数组引用的引用,请使用
@{}
而不是
%{}
。甚至
sub
也可以取消引用

sub foo
{
  print "hello world\n";
}

my %hash = ( call => \&foo );

&{ $hash{call} }; # this allows you to call the sub foo

它是对散列引用数组的引用“散列引用”只是基本的Perl结构,我看到了,并且认为它是一个带有命名字段的记录数组。他们甚至可能在某个时候被祝福到一个类中,
bless($,'SomeClass')foreach@$参数
不能强制数组进入哈希
,并带有严格的和警告。当然是哈希引用数组吗?或者更准确地说,是对散列引用数组的引用。